One of the important job for System Administrators is finding and deleting large and unneeded files from Linux operating system. In this tutorial we will look how to find, sort and delete large and trash files from Linux distributions like Ubuntu, Debian, Mint, Fedora, CentOS and RHEL etc.
Large Files Directories
In Linux distributions there are some directories and paths where files generally holds a lot of disk space. These space are generally used by temporary, cache, log or personal files. Here list of these directories.
Find and Sort Large Files and Directories with du Command
There are alternative ways to find and list directories and files. We will look
du command which is short for disk usage. We will combine commands
sort to sort lines according to size and
head to cut first specified number of lines. In this example we will list files and directories located at
/home/ismail . File and directory sizes are listed in the first columns and we sort with
sort command. We select first
20 line with
$ sudo du -a /home/ismail | sort -n -r | head -n 20
Print Only Directory Summaries
In previous example we will listed a lot of path. This may be consuming. We can list only given path and do not recursively its sub directories with
$ sudo du -s /home/ismail/* | sort -n -r | head -n 20
Skip Files and Directories in Different File Systems
Another useful option provided by
du command is skipping other files systems. As we know in Linux it can be mounted different files systems in to sub directories. We can prevent checking files sizes in this file systems with
-x like below.
$ sudo du -s -x /mnt
Find and Sort Large Files with find Command
find is very useful command used for different purposes. The extensible of
find command provides us to list even delete big files. We print the file and sizes with the
-printf option of the
$ find /home/ismail -printf '%s %p\n'| sort -nr | head -5
Create Alias and Command For Finding Large Files and Directories
As a daily job writing these commands again and agina will be very trivial task. We can create some alias or script from these commands. In this example we will create an alias named
list_big_files alias and add it to the
.bashrc of the current user.
alias list_big_files="sudo du -s /home/ismail/* | sort -n -r | head -n 20"
and we can call
list_big_files like below but in order to make it effective we should start new shell to load our new alias.