30 January 2012

File Test Operators in Shell Script

Leave a Comment
File Test Operators in Shell Script in Linux/Unix


                                       There are many file test Operator which can be used to test various attributes of file like its type , permissions.

1. -f : File exist and is a regular file

$ ls -l
total 112
-rw-r--r-- 1     sandeep sandeep 31 2012-01-01 20:10         abc.lst (regular file)
drwxr-xr-x 2  sandeep sandeep 4096 2011-12-27 13:44    css
drwxr-xr-x 3  sandeep sandeep 4096 2012-01-03 19:50    css1

$ cat > test.sh
#!/bin/bash
if [ -f $1 ] ; then
echo " file is exist"
else
echo "file doesnot exist"
fi

$ chmod 755 test.sh
$ ./test.sh abc.lst               //passing abc.lst as  argument
file exist


2. -r : File exist and it is readable.

3. -w : File exist and it is writable.

4. -x : File exist and it is Executable.

5. -d : File exist and it is directory.

6. -s : File exist and has a size greater than Zero.

7. -e : File exist

8. -u : File exist and has SUID bit set.

9. -k : File exist and has stick bit set.

10. -L : File exist and is a symbolic link.

11. -b : File exist and it is a block device.

12. -c : File exist and it is character device

13. -p : File exist and it is pipe.

14. -S : File exist and it is a Socket file.

15. -g : File exist and has SGID bit set.

16. -O : you are owner of the file.

17. -G : Group-id of file is same as yours.

18. -N : File Modifies since it was last read.

19. f1 -nt  f2 : f1 is newer than f2.

20. f1 -ot f2 : f1 is older than f2.

21. f1 -ef f2 : f1 is linked to f2


If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

0 comments:

Post a Comment