How to check the existence of a file or directory in Bash?

In Bash, you can use the test command or the [ command (which is a synonym for test) to check the existence of a file or directory. Here are a few methods:

  1. Using the -e option with the [ command or the test command:
if [ -e "/path/to/file" ]; then echo "File exists" else echo "File does not exist" fi
  1. Using the -f option to check if a file exists:
if [ -f "/path/to/file" ]; then echo "File exists" else echo "File does not exist" fi
  1. Using the -d option to check if a directory exists:
if [ -d "/path/to/directory" ]; then echo "Directory exists" else echo "Directory does not exist" fi
  1. Using the [[ command, which is an extended version of [ command:
if [[ -e "/path/to/file" ]]; then echo "File exists" else echo "File does not exist" fi

Note that you need to replace "/path/to/file" or "/path/to/directory" with the actual file or directory path you want to check.