Mastering Basic Linux Commands: A Step-by-Step Guide

Introduction:

Linux is a powerful and widely-used operating system that offers a command-line interface for performing various tasks efficiently. In this blog, we will explore essential Linux commands to manipulate files, view their contents, change permissions, and compare file differences. By the end of this guide, you'll have a solid understanding of these fundamental commands.

  1. View File Contents:

To view the contents of a file, we use the 'cat' command. The 'cat' command is short for "concatenate" and is commonly used to display the entire content of a file on the terminal.

Syntax:

cat <filename>

Example:

cat fruits.txt
  1. Change Access Permissions of Files:

Linux provides three types of permissions for files: read, write, and execute. The 'chmod' command is used to modify these permissions.

Syntax:

chmod <permissions> <filename>

Example:

chmod u+x fruits.txt

In this example, 'u' represents the user, and '+x' adds execute permission to the file 'fruits.txt' for the user.

  1. Check Command History:

To view the commands you have executed previously, you can use the 'history' command. It will display a list of recently executed commands with line numbers.

Syntax:

history
  1. Remove a Directory/Folder:

To remove a directory in Linux, we use the 'rmdir' command. Note that the directory must be empty for this command to work.

Syntax:

rmdir <directory_name>

Example:

rmdir my_folder
  1. Create and View the Content of a File:

To create a new file, you can use the 'touch' command. Then, to view its content, use the 'cat' command, as mentioned earlier.

Syntax (Create):

touch <filename>

Example:

touch fruits.txt
  1. Add Content to a File:

To add content to a file, you can use various text editors, but for this guide, we'll use the 'echo' command to append lines to the file.

Syntax (Add content):

echo "content" >> <filename>

Example:

echo -e "Apple\nMango\nBanana\nCherry\nKiwi\nOrange\nGuava" >> devops.txt
  1. Show Only Top Three and Bottom Three Fruits:

To display the top and bottom entries from the 'devops.txt' file, you can use the 'head' and 'tail' commands, respectively.

Syntax (Show top three):

head -n 3 <filename>

Syntax (Show bottom three):

tail -n 3 <filename>

Example:

head -n 3 devops.txt
tail -n 3 devops.txt
  1. Create Another File and View Its Content:

We'll create the 'Colors.txt' file and then use the 'cat' command to view its content, just as we did earlier.

Syntax (Create):

touch Colors.txt

Example:

touch Colors.txt
  1. Add Content to the Colors.txt File:

We'll use the 'echo' command again to add lines to the 'Colors.txt' file.

Syntax (Add content):

echo "content" >> Colors.txt

Example:

echo -e "Red\nPink\nWhite\nBlack\nBlue\nOrange\nPurple\nGrey" >> Colors.txt
  1. Find the Difference Between Two Files:

To find the differences between two files, we use the 'diff' command.

Syntax:

diff <file1> <file2>

Example:

diff fruits.txt Colors.txt

Conclusion:

In this blog, we covered essential Linux commands to view file contents, change access permissions, check command history, remove directories, create and view files, add content to files, display top and bottom entries, and find differences between files. These commands are the building blocks of Linux file manipulation and will prove useful throughout your journey as a Linux user. Practice them regularly to enhance your proficiency and explore the vast capabilities of the Linux command-line interface. Happy learning!