Linux: check if a file is newer than another.

test command is convenient; it can verify if a filename is a file or a directory and many other things, but it can also make age…

Linux: check if a file is newer than another.
Photo by Kelly Sikkema on Unsplash

test command is convenient; it can verify if a filename is a file or a directory and many other things, but it can also make age comparisons between files.

We have the following two files

kpatronas@nautilus:~$ ls -l 
total 20 
-rw-r--r--  1 kpatronas kpatronas     0 Dec  8 22:33 1.txt 
-rw-r--r--  1 kpatronas kpatronas  3314 Dec  6 23:36 config.yml

The -nt option

The -nt option can be used withthe following syntax

$ test <file1> -nt <file2>

if file1 is newer than file2, then test will exit with an exit code of 0, else will exit with 1

$ test 1.txt -nt config.yml 
$ echo $? 
0 
$ test config.yml -nt 1.txt 
$ echo $? 
1

I hope you found this very sort article interesting!