File Handling in Shell Scripting
Use the Bash file test operators -e, -f, -d, -r, -w, -x and -s to check files and directories before a script works on them.
Almost every shell script touches files and directories. It reads a config file, writes into a log folder, runs another script. And almost every script that fails in production fails for the same boring reasons: the file was not there, the folder did not exist, the script had no execute permission.
A careful script checks before it acts. Does the file exist? Is it really a file, or a directory with the same name? Does it have any content? Can I run it? Bash has a set of file test operators for these questions. Each one gives a true or false answer, which you use inside an if.
The file test operators
| Operator | True when | Example |
|---|---|---|
-e | The path exists, file or directory | [ -e "file.txt" ] |
-f | It exists and is a regular file | [ -f "script.sh" ] |
-d | It exists and is a directory | [ -d "/home/user/docs" ] |
-r | It is readable by you | [ -r "file.txt" ] |
-w | It is writable by you | [ -w "file.txt" ] |
-x | It is executable by you | [ -x "script.sh" ] |
-s | It exists and is not empty | [ -s "log.txt" ] |
-L | It is a symbolic link | [ -L "linkname" ] |
A "regular file" is any normal file: a text file, a shell script, a Terraform file, an image. It is the opposite of a directory or a link.
-r, -w and -x answer for the user who runs the script. They are the same read, write and execute permissions you set with chmod in Linux File Permissions.
The spaces inside the brackets are part of the syntax. [ -f "file.txt" ] works, [-f "file.txt"] gives command not found.
Step 1: Does a file exist?
The scripts folder from the earlier lessons has a file called devops.txt. Let's write a script that checks for it.
vi filescript.sh#!/bin/bash
if [ -f "devops.txt" ]
then
echo "File exists"
else
echo "File not found"
fichmod u+x filescript.sh
./filescript.shFile existsStep 2: Does a directory exist?
For a directory the operator is -d. Create a directory to test with:
mkdir projectChange the script:
#!/bin/bash
if [ -d "project" ]
then
echo "Directory exists"
else
echo "Directory not found"
fi./filescript.shDirectory existsA test that only ever says yes proves nothing. Change the name to a directory that is not there, for example demoproject, and run it again:
Directory not foundNow you know both branches work.
-e, -f and -d are not the same
| Path | -e | -f | -d |
|---|---|---|---|
devops.txt (a file) | true | true | false |
project (a directory) | true | false | true |
nothing-here (missing) | false | false | false |
Use -e when you only care that something with that name is there. Use -f or -d when the next command needs one specific kind, for example cat needs a file and cd needs a directory.
Step 3: Check, fix and run
Now a script with a real job. It is given the name of another script and must run it safely:
- If the file is not there, print an error.
- If it is there and executable, run it.
- If it is there but not executable, make it executable first, then run it.
This needs an if inside another if:
vi fileops.sh#!/bin/bash
SCRIPT="run.sh"
if [ -f "$SCRIPT" ]
then
if [ -x "$SCRIPT" ]
then
echo "file is executable and running it ..."
./"$SCRIPT"
else
echo "$SCRIPT is not executable hence making it executable..."
chmod +x "$SCRIPT"
echo "Now running it "
./"$SCRIPT"
fi
else
echo "ERROR : $SCRIPT is not found"
fiThe outer if asks "is there such a file?". Only when the answer is yes does the inner if ask "can I execute it?". Each if has its own fi.
chmod u+x fileops.shCase 1: the file does not exist
There is no run.sh in the folder.
./fileops.shERROR : run.sh is not foundCase 2: the file exists and is executable
script1.sh from an earlier lesson already has the execute permission. In ls output it shows in green. It holds three commands:
#!/bin/bash
pwd
ls
whoamiChange the variable in fileops.sh:
SCRIPT="script1.sh"./fileops.shfile is executable and running it ...
/home/ubuntu/scripts
add.sh arrays.sh devops.txt filescript.sh script1.sh script5.sh
aliens.txt case.sh fileops.sh project telusko.sh
ubuntuAfter the message you see the output of script1.sh: the current directory, the file list and the user name. Your file list will differ.
Case 3: the file exists but is not executable
script5.sh exists but was never given the execute permission. Look at what it does:
cat script5.sh#!/bin/bash
num=5
if ! [ $num -eq 10 ]
then
echo "Not equal to 10"
fiSet SCRIPT="script5.sh" and run:
./fileops.shscript5.sh is not executable hence making it executable...
Now running it
Not equal to 10The script noticed the missing permission, fixed it with chmod +x, and then ran the file. The last line is the output of script5.sh itself.
Two errors from the lecture
Both of these happened while recording, and both are worth understanding.
A space after ./. At one point the run command was typed with a space in it:
./ fileops.sh-bash: ./: Is a directoryThe space splits the command in two. Bash now tries to execute ./, which is the current directory, and passes fileops.sh to it as an argument. A directory cannot be executed, so you get this message. Write ./fileops.sh with nothing in between.
The called script had its own problem. With SCRIPT="add.sh" the output was:
file is executable and running it ...
./add.sh: line 3: +: syntax error: operand expected (error token is "+")
SUM of 2 numbers is :fileops.sh did its job correctly. The error comes from add.sh, which expects two numbers as command line arguments and received none. Read the start of an error line: it names the script that failed.
Why the variable is in quotes
The script writes "$SCRIPT" in double quotes every time. This is not decoration. If a file name contains a space and the variable is not quoted, Bash splits the name into two words:
FILE="my notes.txt"
if [ -f $FILE ][: my: binary operator expectedWith [ -f "$FILE" ] the name stays in one piece. Quote your variables inside [ ] and you avoid this whole group of errors.
Checking for content with -s
A file can exist and still be empty. A backup of zero bytes or an empty report is usually a sign that something went wrong earlier. -s is true only when the file has content:
#!/bin/bash
LOG="app.log"
if [ -s "$LOG" ]
then
echo "$LOG has content"
else
echo "$LOG is empty or missing"
fiNote that -s is false in two situations: the file is empty, or there is no file at all. If you need to tell them apart, test -f first.
Turning a test around
Put ! in front to mean "not". This is the most common way to create a folder only when it is missing:
if [ ! -d "backups" ]
then
mkdir backups
fiAnd you can require two things at once with &&:
if [ -f "$SCRIPT" ] && [ -x "$SCRIPT" ]
then
./"$SCRIPT"
fiThink before a script changes permissions
fileops.sh adds the execute permission by itself. That is a good exercise in nested conditions. In a real environment, be careful with this idea: a file without the execute permission may have been left that way on purpose. A safer script reports the problem and lets a person decide.
Try It Yourself
- Create
filescript.shand test it with a file that exists and one that does not. - Change it to use
-dand test it withprojectand with a directory name that does not exist. - Run
[ -f project ]; echo $?and[ -e project ]; echo $?in the terminal. A result of0means true and1means false. Explain the difference. - Create
fileops.shand go through all three cases: a missing file, an executable script and a script without the execute permission. Check withls -lthat the permission really changed. - Create an empty file with
touch empty.logand write a script that printsEMPTYorHAS DATAusing-s. Then add a line to the file and run it again. - Change
fileops.shso that the script name comes from$1instead of being written in the file.
Official references
Written By: Shramik Masti
How is this guide?
Last updated on
Case Statement in Shell Scripting
Replace long if-elif chains with the Bash case statement, match a value against several patterns, and handle everything else with a default branch.
Auto Backup, System Health Check and Service Monitor
Three small automation scripts in Bash, a timestamped backup, a system health report and a service status checker built with systemctl.
