Bash have programmatic features which gives system administrators a lot of abilities. One of them is
test command which will check given conditions and return logic results like
false . There are a lot of different check functions where we will look most popular and needed ones.
[ EXPRESSION ]
Check If A File Exists
One of the most popular usage of the test command is checking a files existence. We will check if the file named
$ test -e key
Check If A File Is Regular File
In previous example we have checked the files or directory existence. There are different type of files in Linux. One of them which is the most used one is regular file. Regular file contains data like picture, voice, text, SQL data etc.
$ test -f myapp.c
Check If A File Is Directory
We can also check if a file is directory with
$ test -d mnt
Check If A File Is Symbolic& echo “symlink” Link
Symbolic links provides flexibility to use same file in multiple paths as a single file. We can check if the file is symbolic link or not with
$ test -L body2.txt
Check If A File Is Writable
Before trying to write data to file we can check whether the file is writable by the current user. The writable status may change according to current user who try to write. We will use
$ test -w body.txt
Check If A File Is Readable
Another access check is checking if a file is readable. We will use
-r option with the file name in the example below.
$ test -r body.txt
Check If File1 Is Newer Than File2
Another useful check operation is comparing two files. We will compare if
file1 is newer than
file2 . We will use
-nt option in this example.
$ test file1 -nt file2
Check If File1 Is Older Than File2
We can check the reverse of the previous example. We will check if
file1 is older than
-ot option with the following example.
$ test file1 -ot file2