File Handling In Bash Scripting With Exa
Bash scripting is a way to automate tasks on Unix-based operating systems using a scripting language called Bash (Bourne Again Shell). In this article, you will learn how to use bash scripting to handle and test files.
importance of testing for files
It is important that before performing operations on a file, like reading, writing, or deleting, we check if the file exists to prevent the script from encountering errors.
Testing if a file exists before writing to it can prevent accidental overwriting of important data.
We can also test for permissions on a file to know the kind of operations that can be performed on it.
Prerequisites
To fully understand what is being explained in this article, it is important that you have basic knowledge of Bash, the command line, and basic file operations. If you do not have this basic knowledge, kindly check the references in this article to learn about them.
File Testing
File testing in the context of Bash scripting refers to the process of checking specific conditions or properties of files and directories before performing operations on them. This is done using built-in test commands to determine the state, existence, type, and permissions of files or directories. File testing helps ensure that the script handles files correctly and safely, reducing errors and improving reliability.
File testing operators in Bash
Expression | Is True if: |
file_a -ef file_b | file_a and file_b have the inode numbers (the two filenames refer to the same file by hard linking) |
file_a -nt file_b | file_a is newer than file_b |
file_a -ot file_b | file_a is older than file_b |
-b file_a | file exists and is a block-special (device file) |
-w file_a | file exits and is writeable (has write permissions for the effective user) |
-x file_a | file exists and is executable (has execute/search permission for the effective user) |
-d file_a | file exists and is a directory |
-e file_a | file exists |
-f file_a | file exists and is a regular file |
-G file_a | file exists and is owned by the effective group ID |
-k file_a | file exists and has a "sticky bit" set |
-L file_a | file exists and is owned by the effective user ID |
-p file_a | file exists and is a named pipe |
-s file_a | file exists and has a length greater than zero |
-S file_a | file exists and is a network socket |
-u file_a | file exists and is set |
To fully understand this command and how it works, let us create two files named file_a and file_b, respectively.
$ echo "I am file_a" > file_a
That command creates a file named file_a with the content "I am file_a.". We can also follow that command to create file_b by replacing all instances of file_a with file_b in that command.
Using bash scripting to check if two files have the same inode numbers
$ echo "I am a file" > file_a # Create a file with some content
$ ln file_a file_b # Create a hard link named file_b
$ ls -li # List files with their inode numbers
You might get something like this
1234567 -rw-r--r-- 2 user group 12 Aug 25 12:34 file_a
1234567 -rw-r--r-- 2 user group 12 Aug 25 12:34 file_b
Looking at that result, we can see that the two file have the same inode number.
Let us use a simple Bash script to test for other commands.
#!/bin/bash
# test-file: Evaluate the status of a file
FILE=file_a
if [ -e "$FILE" ]; then
if [ -f "$FILE" ]; then
echo "$FILE is a regular file."
fi
if [ -d "$FILE" ]; then
echo "$FILE is a directory."
fi
if [ -r "$FILE" ]; then
echo "$FILE is readable."
fi
if [ -w "$FILE" ]; then
echo "$FILE is writable."
fi
if [ -x "$FILE" ]; then
echo "$FILE is executable/searchable."
fi
else
echo "$FILE does not exist"
exit 1
fi
exit
You can copy that script and test it with different files; doing these will help you understand file handling in bash scripting.
References
Bash File Manipulation with examples