Important Linux Commands

Important Linux Commands

Basics questions on Linux for DevOps

Day 3 of #90DaysOfDevOps with the #TrainWithShubham Community. The challenge is for the DevOps Community to get stronger in #DevOps. It is a great initiative by Shubham Londhe. Documenting my learning and also sharing it with the community is helping me to get more clear with the concepts.

What is the Linux command to?

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

    cat textFile.txt

    You can use the command for concatenating and printing standard file output.

  2. To change the access permissions of files.

    chmod 724 text_file.txt

    On each line, the first character identifies the type of entry that is being listed. If it is a dash (-) it is a file. If it is the letter d it is a directory.

    The next nine characters represent the settings for the three sets of permissions.

    • The first three characters show the permissions for the user who owns the file (user permissions).

    • The middle three characters show the permissions for members of the file’s group (group permissions).

    • The last three characters show the permissions for anyone not in the first two categories (other permissions).

There are three characters in each set of permissions. The characters are indicators for the presence or absence of one of the permissions. They are either a dash (-) or a letter. If the character is a dash, it means that permission is not granted. If the character is an r, w, or an x, that permission has been granted.

The letters represent:

  • r: Read permissions. The file can be opened, and its content viewed.

  • w: Write permissions. The file can be edited, modified, and deleted.

  • x: Execute permissions. If the file is a script or a program, it can be run (executed).

Use chmod is to provide the permissions you wish to give to the owner, group, and others as a three-digit number. The leftmost digit represents the permissions for the owner. The middle digit represents the permissions for the group members. The rightmost digit represents the permissions for the others.

The digits you can use and what they represent are listed here:

  • 0: (000) No permission.

  • 1: (001) Execute permission.

  • 2: (010) Write permission.

  • 3: (011) Write and execute permissions.

  • 4: (100) Read permission.

  • 5: (101) Read and execute permissions.

  • 6: (110) Read and write permissions.

  • 7: (111) Read, write and execute permissions.

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

    history

  2. To remove a directory/ Folder.

    rm -r Test

    rm text_file.txt

    Used without any option rm does not remove directories. To delete an empty directory, use the -d (--dir) option and to delete a non-empty directory, and all of its contents use the -r (--recursive or -R) option.

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

    vim fruits.txt

    cat fruits.txt

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

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

    The append >> operator adds the output to the existing content instead of overwriting it. This allows you to redirect the output from multiple commands to a single file. For example, I could redirect the output to the specifications.txt file by using >> an operator.

  5. Show only the top three fruits from the file.

    head -3 devops.txt

    or

    cat devops.txt | head -3

    It is complimentary to the head command. The tail command, as the name implies, prints the last N number of data of the given input. By default, it prints the last 10 lines of the specified files. If more than one file name is provided then data from each file is preceded by its file name.

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

    tail -3 devops.txt

    or

    cat devops.txt | tail -3

    It is complementary to the Tail command. The head command, as the name implies, prints the top N number of data of the given input. By default, it prints the first 10 lines of the specified files. If more than one file name is provided then data from each file is preceded by its file name.

  7. To create another file Colors.txt and to view the content. Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, and Grey.

    touch Colors.txt

    vim Colors.txt

    cat Colors.txt

  8. To find the difference between the fruits.txt and Colors.txt files.

    diff fruits.txt Colors.txt