Sometimes we need to perform some action if a file is newer than other file. This can be used to recreate generated file if source file has changed. We can use bash check -nt
(newer than) and -oth
(older than) for this.
Bash code to check if file is newer than another file
Use the following code to check if file1.txt is newer than file2.txt
#!/bin/bash if [ "file1.txt" -nt "file2.txt" ]; then echo "file1.txt is newer" fi
Bash code to check if file is older than another file
Use the following code to check if file1.txt is older than file2.txt
#!/bin/bash if [ "file1.txt" -ot "file2.txt" ]; then echo "file1.txt is older" fi