Bash - File test operators
|
|
|
Bash File test operators
Bash has many operators that you can use to test different aspects of a file. For example, you can use the -e operator to ensure that a file exists before deleting it. Or, you can check that a file can be written to before appending to it.
By checking the feasibility of the impending file operation, you can reduce the number of errors that your program will encounter. A complete list of the operators used to test files is given in the reference card below :
| Operator | Tests Whether |
|---|---|
| -e | File exists |
| -s | File is not zero size |
| -a |
file exists This is identical in effect to�-e. It has been�"deprecated," and its use is discouraged. |
| -f | File is a regular file (not a directory or device file) |
| -d | File is a directory |
| -r | File has read permission�( for the user running the test ) |
| -h | File is a symbolic link |
| -w | File has write permission�(for the user running the test) |
| -L | File is a symbolic link |
| -x | File has execute permission�(for the user running the test) |
| -b | File is a block device |
| -c | File is a character device |
| -g |
set-group-id (sgid) flag set on file or directory
If a directory has the sgid flag set, then a file created within that directory belongs to the group that owns the directory, not necessarily to the group of the user who created the file. This may be useful for a directory shared by a workgroup. |
| -p | File is a pipe |
| -u | suid flag set |
| -S | File is a socket |
| -k | "sticky bit"�set |
| -t |
File is associated with a
terminal
This test option may be used to check whether the stdin [ -t 0 ] or stdout [ -t 1 ] in a given script is a terminal. |
| -N | File modified since it was last read |
| F1 -nt F2 | File F1 is newer than F2 * |
| -O | You own the file |
| F1 -ot F2 | File F1 is older than F2 * |
| -G | Group id of file same as yours |
| F1 -ef F2 | Files F1 and F2 are hard links to the same file * |
| ! | NOT (inverts sense of above tests) |
* Binary operator (requires two operands).
On the same subject you can consule the article about the
Bash Special Variables
If you have any remark or questions feel free to put a comment.
If you enjoyed this tutorial and want to promote it don't hesitate to click on