Find oldest files recursively in Linux

Find Files in Linux

Find oldest files within the same folder:

ls -lat | tail -n5

Above command will list all the files in a specific folder while sorting it by modified time using 'ls -lat', the using the pipe symbol to send the output to another command.

Tail -n5 will show only the last 5 results.

How to find oldest files recursively ? (Through the chain of folders' tree/hierarchy)

Your bet is using 'find' command as it searches recurrsively by default, but let us see how to use it's parameters

-type f is used to specify the search type which is file in that case, so in other cases you need to use -type d for directories only
-printf is used to output, and we will use %t to print time and %p to prnt filename while '\n' means new line
sort yes you got it right it's going to sort the output
head -n5 now you can use head to get the top files in the output or use 'tail -n5' or get the opposite while -n5 is the number of output files you want to show, so if you use 'head -n1' it will show only one file.

find /home/dotsway/ -type f -printf '%t+ %p\n' | sort | head -n 5
Output:
Fri Apr 29 10:45:15.0891747285 2016+ /home/dotsway/php.ini
Fri Aug 12 12:19:55.0110672762 2016+ /home/dotsway/php-modules-AfterNaviSiteRecompile.txt
Fri Aug 19 11:21:06.0000000000 2005+ /home/dotsway/perl/MKDoc-Text-Structured-0.83/README


Subscribe to
for video tutorials updates