I encountered an issue of low available disk space on a Ubuntu Linux system. The system had an old 30 GB disk drive and I needed to determine what files were consuming the most space beneath a user's home directory. So I made the user directory the working directory with
cd /home/doe
and used the find
command.
The synatax that can be used for the find command to find all files
greater than a certain size in megabytes (MB) is
find starting_directory -type f -size
+numM -exec ls -lh {} \;
.
starting_directory | Files in and below this directory will be checked. You can use a period to specify the current directory or specify a path name. |
num | A number specifying the file size. You can put a
"b" at the end to use a number in bytes,
a "k" for a number in kilobytes, or an "M" to use a number in
megabytes, e.g. 100M for files greater than 100 megabytes in
size. |
The -exec
parameter tells find to execute the ls -lh
command, the output of which it will check for the specified file
size. The -lh
tells ls
to use a "long" style
display for each line with file sizes displayed in a more human-readable
format.
E.g., the following command will search the current directory and its subdirectories recursively for all files larger than 100 MB in size.
# find . -type f -size +100M -exec ls -lh {} \; -rw-r--r-- 1 doe doe 105M Oct 16 14:26 ./.mozilla/firefox/d0i4yvwz.default/urlclassifier3.sqlite -rw-r--r-- 1 doe doe 634M Nov 19 2012 ./.thunderbird/p8c6q04i.default/global-messages-db.sqlite -rw------- 1 doe doe 12G Nov 16 2012 ./.thunderbird/p8c6q04i.default/ImapMail/192.168.2.5/logs.sbd/daily -rw------- 1 doe doe 160M Apr 8 2012 ./.thunderbird/p8c6q04i.default/ImapMail/192.168.2.5/sent-mail -rw------- 1 doe doe 6.8G Dec 12 2012 ./.thunderbird/p8c6q04i.default/ImapMail/192.168.2.5/INBOX find: `./.gvfs': Permission denied
The above find
command displays the permissions on the files,
the file owner and group, and a timestamp for the file in addition to the
file name and size. If you wish to just display the file names and sizes,
you can pipe the output through awk
to just display the sizes
and file names. The file size appears in the 5th column and the file name
appears in the 9th column, so you can instruct awk
to only
display the data from those two columns.
# find . -type f -size +100M -exec ls -lh {} \; | awk '{print $5 " " $9}' find: `./.gvfs': Permission denied 105M ./.mozilla/firefox/d0i4yvwz.default/urlclassifier3.sqlite 634M ./.thunderbird/p8c6q04i.default/global-messages-db.sqlite 12G ./.thunderbird/p8c6q04i.default/ImapMail/192.168.0.5/logs.sbd/daily 160M ./.thunderbird/p8c6q04i.default/ImapMail/192.168.0.5/sent-mail 6.8G ./.thunderbird/p8c6q04i.default/ImapMail/192.168.0.5/INBOX
In this case, I can see that the largest files are associated with Firefox and Thunderbird.
Note: if you wish to find a file that is exactly a certain size in MB, you would omit the plus sign, "+" before the number. E.g.,:
# find . -type f -size 105M -exec ls -lh {} \; | awk '{print $5 " " $9}' find: `./.gvfs': Permission denied 105M ./.mozilla/firefox/d0i4yvwz.default/urlclassifier3.sqlite
If you wish to find all files below a certain size, you would prefix the number with a minus, "-".