When working on Linux, it’s common to need to verify whether a file exists. For this purpose you can use the ls
command. The ls
command is a simple way to check if a file exists.
ls /path/to/file
Output:
- If the file exists, its name will be displayed.
- If the file does not exist, you’ll see an error like this:
ls: cannot access '/path/to/file': No such file or directory
The following command silently checks a specific file exists on the system and print either “File exists” or “File does not exist” based on the result.
ls /path/to/file >/dev/null 2>&1 && echo "File exists" || echo "File does not exist"