In order to write a cleanup script on a directory, I need to take look at all files that are older than one day. Additionally, I need to delete them in reverse order of modification time (oldest first) until a specified size is reached.
I came along with the following approach to list the files:
find . -mtime +1 -exec ls -a1rt {} +
Am I right, that this does not work for a large number of files (since more than one 'ls' will be executed)? How can I achieve my goal in that case?
You can use the following command to find the 10 oldest files:
find . -mtime +1 -type f -printf '%T@ %p\n' | sort -n | head -10 | awk '{print $2}'
The steps used:
find
, we print the modification timestamp along with the filename.Later if you want to remove them, you can do the following:
rm $(...)
where ... is the command described above.