Check if the file exists on the Linux Shell


Everyone may encounter such a need when they work. How do you check if a file exists in a bash environment of a Unix-like system? Since there is demand, of course there is a solution.

The test command in the shell can be used to detect the type of the file or compare the values. The command can also be used to check whether the file exists, as shown below:

test -e filename
[ -e filename ]
 
test -f filename
[ -f filename ]

A more common use is to place the above command in a conditional expression judged by the if ... else ... fi condition, and then write different branching logic in it:

#!/bin/bash
file="/etc/hosts"
if [ -f "$file" ]
then
    echo "$file found."
else
    echo "$file not found."
fi

The following command uses the shell's conditional expression to determine if the /etc/hosts file exists:

[ -f /etc/hosts ] && echo "Found" || echo "Not found"

This combined command will output the following:

Found

The relevant operators for detecting file attributes:
If the file exists and has the appropriate properties, the following operators will return true :

-b FILE
              FILE exists and is block special

       -c FILE
              FILE exists and is character special

       -d FILE
              FILE exists and is a directory

       -e FILE
              FILE exists

       -f FILE
              FILE exists and is a regular file

       -g FILE
              FILE exists and is set-group-ID

       -G FILE
              FILE exists and is owned by the effective group ID

       -h FILE
              FILE exists and is a symbolic link (same as -L)

       -k FILE
              FILE exists and has its sticky bit set

       -L FILE
              FILE exists and is a symbolic link (same as -h)

       -O FILE
              FILE exists and is owned by the effective user ID

       -p FILE
              FILE exists and is a named pipe

       -r FILE
              FILE exists and read permission is granted

       -s FILE
              FILE exists and has a size greater than zero

       -S FILE
              FILE exists and is a socket

       -t FD  file descriptor FD is opened on a terminal

       -u FILE
              FILE exists and its set-user-ID bit is set

       -w FILE
              FILE exists and write permission is granted

       -x FILE
              FILE exists and execute (or search) permission is granted

The above command was copied from man test.

The method of using the above symbols is exactly the same:

if [ operator FileName ]
then
    echo "FileName - Found, take some action here"
else
    echo "FileName - Not found, take some action here"
fi

There are also three expressions that compare two files to each other:

FILE1 -ef FILE2
              FILE1 and FILE2 have the same device and inode numbers

       FILE1 -nt FILE2
              FILE1 is newer (modification date) than FILE2

       FILE1 -ot FILE2
              FILE1 is older than FILE2
SPOTO CLUB info


About SPOTO CLUB:

SPOTO CLUB focus on online IT Certification training for 16 years. SPOTO CLUB offers 100% real and valid Cisco CCNA, CCNP, CCIE, ISC, Amazon AWS, Microsoft, and other IT exam practice tests. And we have many free online training courses of Cisco exam on YouTube. You can find many useful and helpful tips and suggestions. If you’re still worried about to prepare and pass the Cisco exam, try SPOTO CLUB now. SPOTO CLUB tutors will help you get the CCIE number at the first try.

评论

热门博文