Given a text file containing some list of files, e.g.
$ cat file_list.txt
/var/x/file1.txt
/var/y/file2.txt
<etc>
How can I sort this list of files by some criteria - like their last accessed time, or last changed time?
Thanks in advance.
You can use stat
command with sort
like this:
while read -r line; do
stat -c '%Y %n' "$line"
done < file_list.txt | sort -n | cut -d ' ' -f2
stat -c '%Y %n'
lists time of last modification, seconds since Epoch followed by a space and file namesort -n
sorts timestamps and their filename numericallycut -d ' ' -f2
prints only file names from sort's outputTry one liner (by modification time):
ls -t $(cat file_list.txt)
OR
ls -t `cat file_list.txt`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With