Day 3: Linux Commands Decoded: Elevate Your Tech Game with Simple and Powerful Basics!

Day 3: Linux Commands Decoded: Elevate Your Tech Game with Simple and Powerful Basics!

ยท

4 min read

Introduction

Linux commands may seem daunting at first, but they're the key to navigating and controlling your system. Let's break down some basic Linux commands in simple language:

Tasks

  1. To view what's written in a file.

In Linux, the cat command stands for "concatenate" and is used to display the contents of a file.

  • To display the content of a file:

      cat filename.txt
    

    1. To change the access permissions of files.

the chmod command in Linux is used to change the access permissions of a file or directory.

  • To make a script executable:

      chmod +x script.sh
    
  • before/after changing permission

    1. To check which commands you have run till now.

the history command in Linux is used to display a list of recently executed commands in the terminal.

  • to check history of your commands
history

  1. To remove a directory/folder

the rm -r command in Linux is used to remove directories (folders) and their contents.

  • to remove directory/folder
rm -r foldername/directory_name
  • rm: Stands for "remove," and it is used for deleting files or directories.

  • -r: Stands for "recursive." This option allows rm to delete not only the specified directory but also its entire contents, including subdirectories and files.

  1. Command to create fruits.txt and view to content

To create a file named fruits.txt and view its content, you can use a combination of the touch and cat commands.

  • Create thefruits.txt file:
touch fruits.txt
  • Add some contents to fruits.txt
echo "Apple" >> fruits.txt
echo "Banana" >> fruits.txt
echo "Orange" >> fruits.txt
  • To view the contents of fuits.txt
cat fruits.txt

  1. Add conent in devops.txt (one in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava

You can use a single echo command with the help of a newline character to add all the fruits to the devops.txt file at once.

  • echo -e: Enables interpretation of backslash escapes, including the newline character.

  • "Apple\nMango\nBanana\nCherry\nKiwi\nOrange\nGuava": Represents the list of fruits, each separated by a newline character (\n).

  • > devops.txt: Redirects the output to the devops.txt file, overwriting its content if it already exists.

echo -e "Apple\nMango\nBanana\nCherry\nKiwi\nOrange\nGuava" > devops.txt

  1. To show only top 3 fruits from the file

To show only the top 3 fruits from the devops.txt file, you can use the head command.

  • To show top 3 fruits from devops.txt
head -n 3 devops.txt

  1. To show only bottom 3 fruits from the file

To show only the bottom 3 fruits from the devops.txt file, you can use the tail command.

  • To show bottom 3 fruits from devops.txt
tail -n 3 devops.txt

  1. To create another file colors.txt and to display the content

You can use the touch command to create the Colors.txt file and then use a text editor or the echo command to add content. Here's a set of commands to achieve this:

  • Create theColors.txtfile:

      touch Colors.txt
    

    This command creates an empty file named Colors.txt in the current directory.

  • Add content toColors.txt: You can use a text editor or the echo command to add content. Here's an example using echo:

      echo "Red" >> Colors.txt
      echo "Blue" >> Colors.txt
      echo "Green" >> Colors.txt
      echo "Yellow" >> Colors.txt
    
  • View the content ofColors.txt:

      cat Colors.txt
    

    The cat command displays the content of the Colors.txt file on the terminal.

    The file shown is blank because I have't wrote anything in the file.

  1. Add content in colors.txt (one in each line) - Red, Pink, White, Blue, Purple, Black, Orange, Grey

You can use a single echo command with the help of a newline character to add all the fruits to the colors.txt file at once.

  • echo -e: Enables interpretation of backslash escapes, including the newline character.

  • "Red\nPink\nWhite\nBlue\nPurple\nBlack\nOreange\nGrey": Represents the list of fruits, each separated by a newline character (\n).

  • > colors.txt: Redirects the output to the colors.txt file, overwriting its content if it already exists.

echo -e "Red\nPink\nWhite\nBlue\nPurple\nBlack\nOreange\nGrey" > colors.txt

  1. Find Difference between fruits.txt and colors.txt

To find the differences between the contents of fruits.txt and Colors.txt files, you can use the diff command.

  • To find difference between fruits.txt and colors.txt
diff fruits.txt Colors.txt

Summary:

This comprehensive guide introduces essential Linux concepts and commands in a beginner-friendly manner. Key tasks covered include viewing file contents with the 'cat' command, modifying access permissions using 'chmod,' checking command history with 'history,' and removing directories/folders using 'rm -r.' The guide also explains creating and viewing files, adding content in bulk, and displaying top and bottom lines with 'head' and 'tail' commands. Additionally, it covers creating another file, such as 'Colors.txt,' and finding differences between files using the 'diff' command. Overall, this guide provides a foundational understanding of Linux commands, empowering users to navigate and manage their systems effectively.

"Fuel my passion and support my journey by clicking 'Buy me a coffee' today!"

~Dipen : )

Did you find this article valuable?

Support Dipen's Blog by becoming a sponsor. Any amount is appreciated!

ย