Introduction to Bash File
Bash file is a methodology wherein, bash offers various possibilities of operations to be performed in order to derive meaning out of the extensive data present in a file. This file is a collection of space where data, information, and various other parameters are stored for execution. In the current scenario, there is huge data present everywhere, and deriving important meaning out of them is a daunting task. Operations in a file are specifically for handling these scenarios like a truncation of a file, appending to a file, reading from a file, and many more.
File operations in Bash
As we mentioned in the introduction about the presence of a lot of file operations in bash and here, we would be mentioning about 10 of them which are widely used in the industry. We want to bring to notice that this list won’t be an exhaustive list! So, let us get right into the gist of the article.
1. Truncating a File
This is a method to convert a file to zero sizes. The utility of this bash command is to remove all the content of a file so that only the content is gone for it to be re-filled again but keeping the structure of the folder intact. This is widely done in codes where there is a loop to keep writing contents in a file and this loop needs to run every week. So, one can erase all the content of the file only before writing over it again the next week when it is the time! Here we use the redirection operator “>” which is essentially to overwrite if a file is existing. In case the file is not present a new file is created.
Explanation: At first, we see that the size of file1 is 396 bytes and once we truncate the size is 0 as evident in the shaded output.
2. Appending a string to a file
This methodology is more or less like the truncation method only difference being that we just add a line into the file.
echo "Size of the files in the folder: "
echo "Last line of the file: "
tail -n 1 file1.txt
echo "Adding a new line:"
echo -n "Mauris tempus quis est vitae eleifend." >> file1.txt
Explanation: Here we can see that the new last line has changed from Cras pharetra dolor eu eros iaculis porta to Mauris tempus quis est vitae eleifend.
3. Reading a single line
Now that we have seen how to write into a file it becomes inevitable to understand how to read from a file. Here we would use the keyword read along with the opposite redirection operator “<” to fulfill the utility.
Here -r is to make sure that the line which is read is raw and doesn’t escape and backslash characters. The word variableName is the variable where this line would be stored after it is ready for further use. Though this is one of the ways, there are other ways of reading a file as well.
echo "Reading the first line "
read -r variableName < file1.txt
echo "The first line is stored in the variable is: $variableName"
4. Individual line by line read from a file
This is just an extension of the earlier methodology of reading a line from a file. Here we would need to take help of while loop in fulfilling the ask. What this code eventually does is to read the codes and store in the variableName. This variable now becomes handy to be operated on and this loop goes on till the end of the line or in other words, till the while loop is valid.
echo "Reading each line and printing them one by one"
while read -r variableName; do
echo "Line $count"
done < file1.txt
5. Copy a file
The utility of this command is to copy the file to another location while keeping one at the original location. One uses this utility to keep a backup of the files so that an accidental deletion shouldn’t lead to major consequences.
echo "Copying file1.txt to a backup folder"
echo "Files before copying: "
cp /home/user/file1.txt /home/user/Backup
echo "Files after copying: "
Explanation: Here you would eventually realize that the file1.txt is still present in the original location.
6. Move a file
The utility of this command is similar to the copy, but what differs is that the original file is moved to another location without keeping the original copy in place.
echo "Moving file1.txt to a backup folder"
echo "Files in Backup before copying: "
echo "Files in original folder before copying: "
mv /home/user/file1.txt /home/user/Backup
echo "Files after copying: "
echo "Files in original folder after copying: "
Explanation: Here you would eventually realize that the file1.txt is moved from the original source to the new directory.
7. Finding the size of the file
Many times, it becomes very important to understand the size of a file in order to further understand the health of the disk in terms of free space. Using the below command, we can easily keep track of the size of files and hence the health.
echo "Finding File Size of File2.txt"
file_size=`ls -l file2.txt | cut -d " " -f5`
echo "Size of the file is: $file_size bytes"
In this module, we have looked into all aspects of file operations in bash and would highly encourage you to have hands-on practice of all the commands so that one can get acquainted for the same. In this article, we have gone through all the utilities with an example to mark the effectiveness in learning bash the EduCBa way.
This is a guide to Bash File. Here we discuss an introduction to Bash File, some file operations in detail with sample code in detail explanation. You can also go through our other related articles to learn more –