How to Check If a File or Directory exists in Linux
When you writing a bash script, you may be need to check if a given file or directory exists or not. And this post will guide you how to if a file or directory exists or not in your Linux operating system. How do I test if a file exists using test command in your Linux system.
- Test Command
- Test If File Exists
- Test If Directory Exists
- Test If File Does Not Exist
- Test If Multiple Files Exist
Test Command
Test command can be used to check file types and compare values. Exit with the status determined by EXPRESSION.
The syntax of the Test command
are as followss:
test EXPRESSION test [ EXPRESSION ] [ ] [ OPTION
Options:
-n STRING
the length of STRING is nonzero
-z STRING
the length of STRING is zero
-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
Test If File Exists
If you want to check if a given file named mytest.txt
located in /tmp
directory exists, and you can use one of following statements in your bash script:
if [ -f /tmp/mytest.txt ] then echo "mytest.txt file exists" fi
Or
if test -f /tmp/mytest.txt then echo "mytest.txt file exists" fi
Or
if [[ -f /tmp/mytest.txt ]] then echo "mytest.txt file exists" fi
Or
[ -f /tmp/mytest.txt ] && echo "mytest.txt file exists"
If you want to take another action while file does not exist, you can write it as below:
if [ -f /tmp/mytest.txt ] then echo "mytest.txt file exists" else echo "mytest.txt file does not exists" fi
Or
[ -f /tmp/mytest.txt ] && echo "mytest.txt file exists" || echo "mytest.txt file does not exists"
Test If Directory Exists
If you want to create a new file in a given directory in your bash script, and you need to make sure that that directory exists or not.
For example, you want check a directory /mytmp
exists or not you can use:
if [ -d /mytmp ] then echo "/mytmp directory exists" fi
Or
[ -d /mytmp ] && echo "/mytmp directory exists"
You can also use the test
command to check directory exist or not, see below:
if test -d /tmp/mytest.txt then echo "mytest.txt file exists" fi
Test If File or Directory Does Not Exist
If you only want to check file or directory does not exist firstly, and if true, then you can take some action. you can use the exclamation mark (!
) logical operator in the test experssion in your bash script, see below:
[ !-f /tmp/mytest.txt ] && touch /tmp/mytest.txt
or
[ ! -d /mytmp ] && mkdir -p /mytmp
Test If Multiple Files Exist
If you need to test two or multiple files exist or not, and you can use:
if [ -f /tmp/mytest.txt && -f /tmp/mytest2.txt] then echo "mytest.txt and mytest2.txt file exist" fi
Or
[ -f /tmp/mytest.txt && -f /tmp/mytest2.txt ] && echo "mytest.txt and mytest2.txt file exist"
Conclusion
You should know that how to check if a file or direcotry exists or not using test command in bash shell.