Friday, 8 November 2019

Bash file test operators

Bash file test operators 





-e  FILE      - True if file exists
-a  FILE      - True if file exists  (same as previous, but deprecated)
-r FILE       - True if file exists and is redable by you
-w  FILE     - True if file exists and is writable by you
-x  FILE      - True if file exists and is executable by you

-f   FILE      - True if file exists and file is a regular file (not a directory or device file)




-s   FILE      - True if file exists and not zero size
-d   FILE      - True if file is a directory  
-b   FILE      - True if file is a block device
-c   FILE      - True if file is a characrer device
-h   FILE      - True if file is a symbolic link
-L  FILE      -  True if file is a symbolic link
-S  FILE      -  True if file is a socket
-g  FILE      -  True if set-group-id (sgid) set
-u  FILE      -  True if set-user-id (suid) set
-k  FILE      -  True if sticky bit set
-O  FILE     -  True if you are owner of file
-G  FILE      -  True if group ID of file same as yours
-N  FILE      -  True if file modified since last read
f1 -nt f2        -  True if file f1 is newer than f2
f1 -ot f2        -   True if file f1 is older than f2
f1 -ef f2        -    True files f1 and f2 are hard links to the same file

Example:

device0="/dev/sda2" # / (root directory)
if [ -b "$device0" ] then
     echo "$device0 is a block device."
fi
# /dev/sda2 is a block device.




More info you can get by using the following command:
$ help test|more


No comments: