Bash Shell: Check File Exists or Not

How do I test existence of a text file in bash running under Unix like operating systems?

You need to use the test command to check file types and compare values. The same command can be used to see if a file exist of not. The syntax is as follows:

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

The following command will tell if a text file called /etc/hosts exists or not using bash conditional execution :

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

Sample outputs:


The same code can be converted to use with which allows to make choice based on the success or failure of a test command:

if [ -f "$file" ]
	echo "$file found."
	echo "$file not found."

File test operators

The following operators returns true if file exists:

       -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

(Fig.01: File test operators taken from bash man page)

The syntax is same (see File operators (attributes) comparisons for more info):

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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 11 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersncdu pydf
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
11 comments… add one
  • Mr Xhark Feb 1, 2013 @ 11:07

    Thank you for the trick!

  • Juan Mar 1, 2013 @ 9:21

    thanks much for that useful information

  • CS Major Student Aug 8, 2013 @ 8:37

    Thanks a lot!

  • alex Nov 22, 2013 @ 15:31

    i made a linux script which receives as first argument a path to a directory. I don’t know the path. And i want to check if “file.txt” exists at that certain path . For example :

    if [ -e $1/file.txt ];then echo HAHA fi

  • Ramzii Kerkenir Mar 1, 2015 @ 19:31

    Thank you

  • Omur May 25, 2015 @ 16:44


    if you use -f with *.sock files it always says “the file does not exist”. when I use -e it worked :)

  • Jon Slavin Jul 22, 2015 @ 15:07

    What if I want to test for the existence of any files with a particular filename extension? Is there a simple way to do that? I’ve trying to use the return status of the ls command but I seem to be getting the syntax a bit wrong.

    • Erie Feb 3, 2016 @ 8:55

      You can use wildcard. Ex:
      [ -e *”.extension” ]

      (don’t forget to have a space character before the operator and before the closing branch, it took hours of my time to recognize this :| )

  • Erie Feb 3, 2016 @ 8:45

    Can I mix the operator?
    if [-xw filename]
    (Checking the existence of the file that executable and writable)

  • manoj kumar S D May 10, 2016 @ 13:37

    hello ,

    i want to write script to copy a some of the files inside directory to specified location(for example i have one directory, this containes 1000 image file. then i need pick only 100 image file from 1000 image file. also i have a list of images which i need to copy (i made one file).

    can anyone help on this(please forword to my mail)


  • vijay Feb 1, 2017 @ 3:50

    in my script I have[ $FILE ! = “”] ..what does it mean…

    FILE has lot ma y rows extracted file names from database.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum