Numerous scenarios will arise where you may need to perform an action based on whether a file exists or not.

While using the test command in Bash, you should determine whether a file exists and determine its file type.

A test command can take one of three possible syntax:

Test expression.

[ EXPRESSION ]

[[ EXPRESSION ]]

If you want a script to be portable, you should use the available command on all POSIX shells. The latest version of the test command, [[ (double brackets), is supported on most modern systems using the Bash, Zsh, and Ksh as a default shell.

Check if the file exists.

When checking a specific file, the most commonly used FILE operations include -e and -f. The first one will verify any file’s existence regardless of what type of file it is, while the second one will only return true for those files that are regular files (not a directory or a device).

The most efficient method of determining whether a file exists uses the test command and the if statement. Suppose you see any of the following, the /etc/resolv.conf file exists:

FILE=/etc/resolv.conf

if test -f "$FILE"; then

echo "$FILE exists."

fi
FILE=/etc/resolv.conf

if [ -f "$FILE" ]; then

echo "$FILE exists."

fi
FILE=/etc/resolv.conf

if [[ -f "$FILE" ]]; then

echo "$FILE exists."

fi

If you want to deliver a different action based on whether the file exists or not, simply use the if/then construct:

FILE=/etc/resolv.conf.

if [ "$FILE" ]; then

echo "$FILE exists."

else.

echo "$FILE does not exist."

fi

You can also operate a printer without the need for an if statement. The command following the && operator is only executed if the test command’s exit code is valid.

test -f /etc/resolv.conf && echo "$FILE exists."
[ -f /etc/resolv.conf ] && echo "$FILE exists."
[[ -f /etc/resolv.conf ]] && echo "$FILE exists."

When performing a series of commands using the && operator, make sure the semicolon does not separate them.

[ -f /etc/resolv.conf ] && { echo "$FILE exist."; cp "$FILE" /tmp/; }

What is said after the || operator is only valid as long as the test command’s exit status is false.

[ -f /etc/resolv.conf ] && echo "$FILE exist." || echo "$FILE does not exist."

Verify that Directory Exists.

The -d test tools you to quickly determine whether a file is a directory.

To check whether Docker’s /etc. The directory is present, use:

FILE=/etc/docker

if [ -d "$FILE" ]; then

echo "$FILE is a directory."

fi

[ -d /etc/docker ] && echo "$FILE is a directory."

Also, you can use the double brackets [[ instead of the single brackets [.

Check if the file already exists.

Moreover, the test expression can be negated using the word not. ! logical not operator:

FILE=/etc/docker

if not ( -f "$FILE" ]; then

echo "$FILE does not exist."

fi

Similar to above:

[ ! -f /etc/docker ] && echo "$FILE does not exist."

Check for multiple files.

Instead of using complex nested if/else statements, you can write a little more simply by using logic like this:

if [ -f /etc/resolv.conf -a -f /etc/hosts ]; then
echo "Both files exist."

fi

if [[ -f /etc/resolv.conf && -f /etc/hosts ]]; then

echo "Both files exist."

fi

Without using the IF statement:

[ -f /etc/resolv.conf -a -f /etc/hosts ] && echo "Both files exist."

[[ -f /etc/resolv.conf && -f /etc/hosts ]] && echo "Both files exist."

File test operators.

The test command includes some of the following file operators, which indicate whether a file is of a particular type.

  • -b FILE - True if that FILE exists and is also a special block file.
  • -c FILE - True if that FILE exists and is also a special character file.
  • -d source - True if source exists and is also a directory.
  • -e FILE - True if that FILE exists and is also a file, regardless of the type of (node, socket, directory, etc.).
  • -f FILE - True if that FILE exists and is also a regular file (not a device or directory).
  • -G FILE - True if that FILE exists and has the same group as the user is running that command.
  • -h FILE - True if that FILE exists and is also a symbolic link.
  • -g FILE - True if that FILE exists and also has set-group-id (sgid) flag set.
  • -k FILE – True if that FILE exists and also has a sticky bit flag set.
  • -L FILE – True if that FILE exists and is also a symbolic link.
  • -O FILE – True if that FILE exists and is also owned by the user running a command.
  • -p FILE – True if that FILE exists and is also a pipe.
  • -r FILE – True if that FILE exists and is also readable.
  • -S FILE – True if that FILE is also a socket.
  • -s FILE – True if that FILE exists and also has nonzero size.
  • -u FILE – True if that FILE exists, and also set-user-id (suid) flag is set.
  • -w FILE – True if that FILE exists and is also writable.
  • -x FILE – True if that FILE exists and is also executable.

Leave a Reply

Your email address will not be published. Required fields are marked *