Updated May 20, 2023
Introduction to Bash File
Bash file is a methodology wherein bash offers various possibilities of operations to be performed 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 truncating 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 will be saying about 10 of them, which are widely used in the industry. We want to inform you that this list won’t be exhaustive! 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 to keep 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 file’s content only before writing over it again the next week when it is time! Here we use the redirection operator “>,” essentially to overwrite if a file exists. 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 is that we add a line to the file.
echo "Size of the files in the folder: " ls -lh 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 and the opposite redirection operator “<” to fulfill the utility.
Here -r ensures the line 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.
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. This code eventually reads the codes and stores them in the variableName. This variable becomes handy to be operated on, and this loop goes on till the end of the line or until the while loop is valid.
echo "Reading each line and printing them one by one" count=1 while read -r variableName; do echo "Line $count" echo $variableName count=$((count+1)) 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 significant consequences.
echo "Copying file1.txt to a backup folder" echo "Files before copying: " ls /home/user/Backup cp /home/user/file1.txt /home/user/Backup echo "Files after copying: " ls /home/user/Backup
Explanation: You will eventually realize that file1.txt is still in the original location.
6. Move a file
This command’s utility resembles the copy command, but the difference is that it moves the original file to a new location without leaving a copy behind.
echo "Moving file1.txt to a backup folder" echo "Files in Backup before copying: " ls /home/user/Backup echo "Files in original folder before copying: " ls /home/user mv /home/user/file1.txt /home/user/Backup echo "Files after copying: " ls /home/user/Backup echo "Files in original folder after copying: " ls /home/user
Explanation: Eventually, you’ll realize that we’ve moved file1.txt from its original source to the new directory.
7. Finding the size of the file
It is often essential to understand the size of a file to further understand the disk’s health 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 with all the commands to get acquainted. In this article, we have gone through all the utilities with an example to mark the effectiveness of learning bash the EduCBa way.
We hope that this EDUCBA information on “Bash File” was beneficial to you. You can view EDUCBA’s recommended articles for more information.