Basic Linux Commands

Day 3 : 90Days of DevOps Challenge

ยท

8 min read

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

    -> you can use the "cat" command in Linux.

    Syntax :

     cat [file]
    

    Example :

    If you want to view the contents of a file named "example.txt", you can type the following command in the terminal:

     cat example.txt
    
  2. To change the access permissions of files.

    -> you can use the "chmod" command in Linux. The chmod command allows you to modify the file access permissions for the owner, group, and other users.

    Syntax :

     chmod [permissions] [file]
    

    Here, the "permissions" parameter is a three-digit code that represents the desired permissions for the owner, group, and other users.

    The first digit represents the permissions for the owner, the second digit represents the permissions for the group, and the third digit represents the permissions for other users.

    Each digit is calculated by adding the binary values of the desired permissions:

    • Read: 4

    • Write: 2

    • Execute:1

Example :

if you want to grant read and write access to the owner, read-only access to the group, and no access to other users, you would use the code "640".

    chmod 640 example.txt
  1. To check which commands you have run till now.

    -> To check the list of commands that you have run previously in the terminal, you can use the "history" command in Linux.

    Syntax :

     history
    

    This command will display a numbered list of all the commands that you have run in the terminal, starting from the most recent ones.

    Example :

    If you want to view the list of commands that you have run previously, simply type the following command in the terminal: history

    This will display a numbered list of all the commands that you have run in the terminal. If you want to repeat any of the previous commands, you can simply type its corresponding number and press Enter.

  2. To remove a directory/ Folder.

    -> To remove a directory or folder in Linux, you can use the "rmdir" or "rm" command.

    The "rmdir" command is used to remove empty directories only. If the directory is not empty, you can use the "rm" command with the "-r" option to remove the directory and all its contents recursively.

    Syntax :

    To remove an empty directory using "rmdir":

     rm -r [directory]
    

    Example :

    Suppose you want to remove a directory named "my_folder" that is located in your home directory. Here's how you can do it:

    If the directory contains files or other subdirectories, you can use "rm -r" to remove it along with its contents:

     rm -r my_folder
    

    Note that the "rm" command is a powerful command and it can delete files and directories permanently, so use it with caution.

  3. To create a fruits.txt file and to view the content.

    -> To create a new file named "fruits.txt" in Linux and view its contents, you can use the "touch" and "cat" commands.

    Syntax :

    To create a new file using "touch":

     touch [filename]
    

    To view the contents of a file using "cat":

     cat [filename]
    

    Example :

    To create a new file named "fruits.txt" in your current directory, you can use the following command:

     touch fruits.txt
    

    This will create a new file named "fruits.txt" in your current directory. To view the contents of this file, you can use the "cat" command as follows:

     cat fruits.txt
    

    If the file is empty, the "cat" command will not display anything. You can add some content to the file using a text editor or by redirecting output from the terminal to the file. For example, you can use the following command to add the names of some fruits to the file:

     echo "apple" >> fruits.txt
     echo "banana" >> fruits.txt
     echo "orange" >> fruits.txt
    

    Now, if you use the "cat" command again, you will see the contents of the "fruits.txt" file:

     cat fruits.txt
    

    Output :

     apple
     banana
     orange
    
  4. Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.

    -> To add the given content "Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava" (one in each line) to a file named "devops.txt" in Linux, you can use a text editor or the "echo" command with the append operator ">>".

    Syntax:

    To append content to a file using "echo" and ">>":

     echo "[content]" >> [filename]
    

    Example :

    To add the given content to a new or existing file named "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 of the fruits to a new line in the "devops.txt" file. You can verify the contents of the file by using the "cat" command as follows:

     cat devops.txt
    

    Output:

     Apple
     Mango
     Banana
     Cherry
     Kiwi
     Orange
     Guava
    
  5. To Show only top three fruits from the file.

    To show only the top three fruits from the "devops.txt" file in Linux, you can use the "head" command with the "-n" option to specify the number of lines to display.

    Syntax :

    To display the first n lines of a file using "head":

     head -n [number] [filename]
    

    Example :

    To show only the top three fruits from the "devops.txt" file, you can use the following command:

     head -n 3 devops.txt
    

    This will display the first three lines of the "devops.txt" file, which contain the names of the top three fruits:

    Output:

     Apple
     Mango
     Banana
    

    Note that the "head" command displays the first n lines of a file by default. If you want to display a different number of lines, you can specify it using the "-n" option followed by the number of lines you want to display.

  6. To Show only bottom three fruits from the file.

    To show only the bottom three fruits from the "devops.txt" file in Linux, you can use the "tail" command with the "-n" option to specify the number of lines to display from the end of the file.

    Syntax :

    To display the last n lines of a file using "tail":

     tail -n [number] [filename]
    

    Example :

    To show only the bottom three fruits from the "devops.txt" file, you can use the following command:

     tail -n 3 devops.txt
    

    This will display the last three lines of the "devops.txt" file, which contain the names of the bottom three fruits:

    Output :

     Kiwi
     Orange
     Guava
    

    Note that the "tail" command displays the last n lines of a file by default. If you want to display a different number of lines, you can specify it using the "-n" option followed by the number of lines you want to display.

  7. To create another file Colors.txt and to view the content.

    To create a new file named "Colors.txt" in Linux and view its contents, you can use the "touch" and "cat" commands.

    Syntax :

     touch [filename]
    

    To view the contents of a file using "cat":

     cat [filename]
    

    Example :

    To create a new file named "Colors.txt" in your current directory, you can use the following command:

     touch Colors.txt
    

    This will create a new file named "Colors.txt" in your current directory. To view the contents of this file, you can use the "cat" command as follows:

     cat Colors.txt
    

    If the file is empty, the "cat" command will not display anything. You can add some content to the file using a text editor or by redirecting output from the terminal to the file. For example, you can use the following command to add some colors to the file:

     echo "red" >> Colors.txt
     echo "green" >> Colors.txt
     echo "blue" >> Colors.txt
    

    Now, if you use the "cat" command again, you will see the contents of the "Colors.txt" file:

     cat Colors.txt
    

    Output :

     red
     green
     blue
    
  8. Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.

    To add the given content "Red, Pink, White, Black, Blue, Orange, Purple, Grey" (one in each line) to an existing file named "Colors.txt" in Linux, you can use a text editor or the "echo" command with the append operator "\>>".

    Syntax :

    To append content to a file using "echo" and ">>":

    echo "[content]" >> [filename]
    

    Example :

    Assuming that you have already created the "Colors.txt" file and it contains the content "red, green, blue", you can add the given content to the file using the following commands:

    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 add each of the colors to a new line in the "Colors.txt" file. You can verify the contents of the file by using the "cat" command as follows:

    cat Colors.txt
    

    Output :

    red
    green
    blue
    Red
    Pink
    White
    Black
    Blue
    Orange
    Purple
    Grey
    
  9. To find the difference between fruits.txt and Colors.txt file.

    Syntax :

    To find the difference between two files using "diff":

    diff [file1] [file2]
    

    Example :

    Assuming that you have the "fruits.txt" and "Colors.txt" files in your current directory, you can use the "diff" command to find the differences between them as follows:

    diff fruits.txt Colors.txt
    

    This will display the differences between the two files, if any. The output will show lines that are unique to each file and lines that are common to both files. Each line will be prefixed with "<" or ">" to indicate which file it belongs to.

    For example, if the "fruits.txt" file contains the following lines:

    Apple
    Mango
    Banana
    Cherry
    Kiwi
    

    And the "Colors.txt" file contains the following lines:

    Red
    Pink
    White
    Black
    Blue
    Orange
    Purple
    Grey
    

    The output of the "diff" command would be:

    3a4,9
    > Red
    > Pink
    > White
    > Black
    > Blue
    > Orange
    > Purple
    

Thank You for Reading the Blog.. ๐Ÿ‘

connect with me : linkedin.com/in/shivraj-salunkhe-5881141a4

follow my blog channel : shivrajofficial.hashnode.dev

ย