Found several posts like this one to tell how to find the latest file inside of a folder.
My question is one step forward, how to find the second latest file inside the same folder? The purpose is that I am looking for a way to diff
the latest log with a previous log so as to know what have been changed. The log was generated in a daily basis.
To look at the last few lines of a file, use the tail command. tail works the same way as head: type tail and the filename to see the last 10 lines of that file, or type tail -number filename to see the last number lines of the file. Try using tail to look at the last five lines of your .
The ls command lists files in the current working directory. To move a file from one directory to another, use mv. The use of the mv command changes the name of the file from oldname to newname. Each of the commands listed above have options that may be specified on the command line.
/var/log. This is such a crucial folder on your Linux systems. Open up a terminal window and issue the command cd /var/log. Now issue the command ls and you will see the logs housed within this directory (Figure 1).
Building on the linked solutions, you can just make tail
keep the last two files, and then pass the result through head
to keep the first one of those:
ls -Art | tail -n 2 | head -n 1
To do diff
of the last (lately modified) two files:
ls -t | head -n 2 | xargs diff
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