Monday, September 19, 2011

Get Size of Files in a Linux Directory Modified in the Past 24 Hours

SkyHi @ Monday, September 19, 2011
find . -mtime -27 -type f -printf "%p %sBytes\n"

With a system thats regularly creating files on-the-fly it’s important to keep track of the filesizes to monitor disk usage. One of the daily tasks I perform is to track the filesize of files created within the last day by running the following on the command line:
view plaincopy to clipboardprint?

find /directory_name -iname "*.zip" -mtime -1 -printf "%p %k\n"

By running the above you will be presented with a list of all ZIP files modified in the last 24 hours along with the filesize of each. Let me break it down a bit further:

find

‘find’ is a linux command used to locate files on the Linux system. Used in conjunction with additional parameters it can be a powerful command to find a set of specific files.

/directory_name

This is the directory that we wish to search within. All subdirectories of this specified folder will also be included in the search.
view plaincopy to clipboardprint?

-iname "*.zip"

Used to locate all files with a ‘.zip’ extension.

-mtime -1

Here we specify the modified date of the files that we wish to return. In this scenario we are saying show all files modified within the last 1 day.
view plaincopy to clipboardprint?

-printf "%p %k\n"

This is the format of the files output to screen. %p correponds to the filename and %k relates to the size of the file in kilobytes. Each file will be separated by a new line (\n).


REFERENCES
http://biostall.com/get-size-of-files-in-linux-directory-modified-in-past-24-hours