3. Essential Linux Commands for File Management and Manipulation

3. Essential Linux Commands for File Management and Manipulation

Linux, being a powerful and versatile operating system, offers a multitude of commands for various tasks. In this article, we will explore some essential Linux commands that can help you manage and manipulate files and directories effectively. We will cover everything from viewing file content to comparing the contents of two files.

Viewing File Content

To view the contents of a file in Linux, you can use the cat command. For example, to see the contents of a file called example.txt, you would enter the following command:

cat example.txt

This command displays the entire content of the file on your terminal.

Changing File Access Permissions

Linux provides the chmod command to change file access permissions. You can modify the permissions for a file by specifying the desired permissions and the file's name. For example, to give read and write permissions to the owner of a file called file.txt, you would use the following command:

chmod u+rw file.txt

Checking Command History

To check the history of commands you have run in your terminal session, you can use the history command. Simply type:

history

This command will display a list of previously executed commands along with their respective line numbers.

Removing a Directory or Folder

If you need to remove a directory and its contents, you can use the rm command with the -r option. For example, to delete a directory named myfolder, you would enter:

rm -r myfolder

Creating and Viewing File Content

You can create a new file and view its content using the touch and cat commands. To create a file called fruits.txt and view its content, follow these steps:

touch fruits.txt cat fruits.txt

This will create an empty fruits.txt file and display its contents, which will be nothing initially.

Adding Content to a File

To add content to a file, you can use the echo command, which appends text to the specified file. For example, to add the names of fruits one by one to a file called devops.txt, you can use the following commands:

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

This will add each fruit to a new line in the devops.txt file.

Showing Top and Bottom Entries

If you want to see only the top three or bottom three entries in a file, you can use the head and tail commands. For instance, to display the top three fruits in devops.txt, you can use:

head -n 3 devops.txt

And to display the bottom three fruits, use:

tail -n 3 devops.txt

Creating and Adding Content to Another File

To create a new file called Colors.txt and add colors to it, you can follow these steps:

touch Colors.txt 
echo "Red" >> Colors.txt
echo "Pink" >> Colors.txt
echo "White" >> Colors.txt
echo "Black" >> Colors.txt
echo "Blue" >> Colors.txt
echo "Orange" >> Colors.txt
echo "Purple" >> Colors.txt
echo "Grey" >> Colors.txt

This will create the Colors.txt file and populate it with the color names.

Finding the Difference Between Files

To find the difference between the contents of two files, you can use the diff command. For example, to compare the fruits.txt and Colors.txt files, you would run:

diff fruits.txt Colors.txt

The diff command will display the lines that differ between the two files.

These essential Linux commands empower you to perform a wide range of file and directory management tasks. Whether you need to view file contents, change permissions, manage directories, or compare file contents, Linux provides the tools you need to get the job done efficiently.

Thank you for reading this article.

Did you find this article valuable?

Support Explore DevOps by becoming a sponsor. Any amount is appreciated!