A Beginner's Guide to File Management in Linux

As a DevOps enthusiast, I am embarking on a journey to become a skilled DevOps Engineer, and I plan to share my learning experiences through blog posts. As a DevOps Engineer, my primary goal is to bridge the gap between development and operations teams by leveraging automation, collaboration, and communication to streamline software development and deployment processes. I recognize that DevOps is not just a set of tools and technologies but also a cultural shift that emphasizes collaboration, agility, and a customer-centric approach. Therefore, in addition to technical skills, I will also focus on soft skills such as communication, teamwork, and problem-solving.
Through my blog posts, I aim to share my journey, insights, challenges, and successes as I navigate the world of DevOps. My goal is to provide value to my readers by offering practical advice, tips, and tricks, and inspiring them to embark on their own DevOps journey.
Working with files is an essential part of any operating system. Linux provides a variety of commands to manage files, including creating, modifying, and deleting files. In this blog, we will learn how to view the contents of a file, change file permissions, remove a directory, create a new file, and append data to a file. We will also explore how to find the difference between two files.
1) To view what's written in a file.
To view the contents of a file in Linux, you can use various commands based on your requirements. Here are a few commonly used commands:
- cat: The cat command is used to concatenate and display files. To view the contents of a file using cat, open a terminal and type the following command:
cat filename
Replace filename with the name of the file you want to view. This will display the entire content of the file on the terminal.
- less: The less command is used to view a file one page at a time. To use less, open a terminal and type the following command:
less filename
This will open the file in less, and you can scroll up and down using the arrow keys. To exit less, press q.
- more: The more command is similar to less, but it displays the file one page at a time in the opposite direction. To use more, open a terminal and type the following command:
more filename
This will open the file in more, and you can scroll up and down using the arrow keys. To exit more, press q.
- head: The head command is used to display the first few lines of a file. To use head, open a terminal and type the following command:
head filename
This will display the first 10 lines of the file on the terminal. You can also specify the number of lines you want to display using the -n option. For example, to display the first 5 lines of the file, use the following command:
head -n 5 filename
2) To change the access permissions of files.
Changing file permissions File permissions can be changed using the chmod command. The chmod command allows us to set the read, write, and execute permissions for the owner, group, and others. For example, to give the owner of a file read and write permissions, we would run the following command:
chmod [reference][operator][mode] file...
The references are used to distinguish the users to whom the permissions apply i.e. they are list of letters that specifies whom to give permissions. The references are represented by one or more of the following letters:
| Reference | Class | Description |
| u | owner | file's owner |
| g | group | users who are members of the file's group |
| o | others | users who are neither the file's owner nor members of the file's group |
| a | all | All three of the above |
The operator is used to specify how the modes of a file should be adjusted. The following operators are accepted:
| Operator | Description |
| + | Adds the specified modes to the specified classes |
| - | Removes the specified modes from the specified classes |
| \= | The modes specified are to be made the exact modes for the specified classes |
The modes indicate which permissions are to be granted or removed from the specified classes. There are three basic modes that correspond to the basic permissions:
The modes indicate which permissions are to be granted or removed from the specified classes. There are three basic modes which correspond to the basic permissions:
| r | Permission to read the file |
| w | Permission to write (or delete) the file. |
| x | Permission to execute the file, or, in the case of a directory, search it. |
3) To check which commands you have run till now.
Checking command history To view the commands that have been run in the current session, we can use the history command. For example, to view the last 10 commands that were run, we would run the following command:
history 10
4) To remove a directory/ Folder.
Removing a directory To remove a directory, we can use the rmdir command. For example, to remove a directory called dir, we would run the following command:
rmdir dir
5) To create a fruits.txt file and to view the content.
Creating a file To create a new file, we can use the touch command. For example, to create a file called fruits.txt, we would run the following command:
touch fruits.txt
6) Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.
Viewing the contents of a file To view the contents of a file, we can use the cat command. For example, to view the contents of a file called fruits.txt, we would run the following command:
cat fruits.txt
7) To Show only top three fruits from the file.
Appending data to a file To append data to a file, we can use the echo command followed by the >> operator. For example, to add the fruits Apple, Mango, Banana, Cherry, Kiwi, Orange, and Guava to a file called devops.txt, we would run the following command:
echo "Apple" >> devops.txt
echo "Mango" >> devops.txt
echo "Banana" >> devops.txt
echo "Cherry" >> devops.txt
echo "Kiwi" >> devops.txt
echo "Orange" >> devops.txt
echo "Guava" >> devops.txt
8) To Show only bottom three fruits from the file.
Showing top three fruits from a file To show the top three fruits from a file, we can use the head command. For example, to show the top three fruits from a file called devops.txt, we would run the following command:
head -n 3 devops.txt
Showing bottom three fruits from a file To show the bottom three fruits from a file, we can use the tail command. For example, to show the bottom three fruits from a file called devops.txt, we would run the following command:
tail -n 3 devops.txt
9) To create another file Colors.txt and to view the content.
Creating another file To create another file, we can use the touch command. For example, to create a file called Colors.txt, we would run the following command:
touch Colors.txt
To view the contents of the newly created file, use the cat command:
cat Colors.txt
10) Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.
To Add Content in Colors.txt (One in Each Line)
Use the echo command to add the words "Red", "Pink", "White", "Black", "Blue", "Orange","Purple","Grey"
echo " Red, Pink, White, Black, Blue, Orange, Purple, Grey" > Colors.txt
11) To find the difference between fruits.txt and Colors.txt file.
diff stands for difference. This command is used to display the differences in the files by comparing the files line by line. Unlike its fellow members, cmp and comm, it tells us which lines in one file have is to be changed to make the two files identical.
diff Colors.txt fruits.txt
Happy Learning 😄
Bhaktiben Kadiya




