Linux method to check whether the file exists: 1. Use the test command to check, the syntax "test -e <file>"; 2. Use the bash shell command to check, the syntax "[-f file path ] && echo "found" || echo "not found"".
The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.
Linux checks whether the file exists
It is often used to test conditional expressions, using The command is test -e <file>
.
Assume we have the file directory /home/benben/go_project
and the file /home/benben/go_project/test.txt
, below we use the bash shell command to check whether this directory and file exist.
[-d /home/benben/go_project ] && echo "found" || echo "not found"
or [-d . /go_project ] && echo "found" || echo "not found"
. [-f /home/benben/go_project/test.txt] && echo "found" || echo "not found"
or [-f ./test.txt ] && echo "found" || echo "not found"
Related recommendations: "Linux Video Tutorial"
The above is the detailed content of How to check if a file exists in linux. For more information, please follow other related articles on the PHP Chinese website!