table of Contents
2.1 -d file detection directory
2.2 -f file detects ordinary files
2.3 -x file detect file executable
2.4 -e file detects the existence of a file/directory
2.5 -b file detects block device files
In Shell programming, the file test operator is widely used, and it is often necessary to detect the attributes of a file, which will be explained with examples below.
One, the file test operator
Operator | Description |
---|---|
-b file | Check whether the file is a block device file, and return true if it is. |
-c file | Check whether the file is a character device file, and return true if it is. |
-d file | Check whether the file is a directory, and return true if it is. |
-f file | Check whether the file is a normal file (neither a directory nor a device file), and return true if it is. |
-g file | Check whether the SGID bit is set in the file, and return true if it is. |
-in the file | Check whether the SUID bit is set in the file, and return true if it is. |
-k file | Check whether the file has a sticky bit (Sticky Bit), if so, return true. |
-p file | Check whether the file is a well-known pipe, and return true if it is. |
-r file | Check if the file is readable, and return true if it is. |
-w file | Check whether the file is writable, and return true if it is. |
-x file | Check if the file is executable, and return true if it is. |
-s file | Check whether the file is empty (whether the file size is greater than 0), return true if it is not empty. |
-e file | Check whether the file (including directory) exists, and if so, return true. |
2. Common examples
2.1 -d file detection directory
#!/bin/bash
file="/root"
if [ -d $file ]
then
echo "$file is directory!"
else
echo "$file is not directory!"
fi
Output:
[root@localhost Shell]# ./dir.sh
/root is directory!
[root@localhost Shell]#
2.2 -f file detects ordinary files
#!/bin/bash
file="/root/regularFile"
if [ -f $file ]
then
echo "$file is regular file!"
else
echo "$file is not regular file!"
fi
Output:
[root@localhost Shell]# ./dir.sh
/root/regularFile is regular file!
[root@localhost Shell]#
2.3 -x file detect file executable
#!/bin/bash
file="/root/Shell/dir.sh"
if [ -x $file ]
then
echo "$file is executable file!"
else
echo "$file is not executable file!"
fi
Output:
[root@localhost Shell]# ls -l dir.sh
-rwxr-xr-x. 1 root root 145 2月 6 10:54 dir.sh
[root@localhost Shell]# ./dir.sh
/root/Shell/dir.sh is executable file!
[root@localhost Shell]#
2.4 -e file detects the existence of a file/directory
#!/bin/bash
file="/root"
if [ -e $file ]
then
echo "$file is exist!"
else
echo "$file is not exist!"
fi
Output:
[root@localhost Shell]# ./dir.sh
/root is exist!
[root@localhost Shell]#
2.5 -b file detects block device files
#!/bin/bash
if [ -b /dev/sda ]
then
echo "/dev/sda is Block Device File!"
else
echo "/dev/sda is not Block Device File!"
fi
The output is:
[root@localhost Shell]# ./dir.sh
/dev/sda is Block Device File!
[root@localhost Shell]#
Three, summary
File test operators are often used in Shell programming, and several commonly used operators should be remembered.
references: