Home > Operation and Maintenance > Linux Operation and Maintenance > How to check if a file exists in linux

How to check if a file exists in linux

青灯夜游
Release: 2021-12-02 17:39:29
Original
25859 people have browsed it

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"".

How to check if a file exists in linux

The operating environment of this tutorial: linux5.9.8 system, Dell G3 computer.

Linux checks whether the file exists

1. test command

It is often used to test conditional expressions, using The command is test -e <file>.

2. Use bash shell to check

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.

  • Check the directory and execute the command [-d /home/benben/go_project ] && echo "found" || echo "not found"or [-d . /go_project ] && echo "found" || echo "not found".
  • Check the file and execute the command[-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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template