Skip to main content

Command Palette

Search for a command to run...

Day 03 Task: Basic Linux Commands🚀🥇🔐✏

Updated
6 min readView as Markdown
Day 03 Task: Basic Linux Commands🚀🥇🔐✏

Task: What is the Linux command to

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

    • Use the cat command.

    • Example: To view the contents of a file named devops.txt, type cat devops.txt and press Enter.

        ubuntu@ip-172-31-38-2:~$ cat devops.txt
        hello welcome to the world of devops
      
  2. To change the access permissions of files.

    • Use the chmod command with permission parameters(read[r],write[w],execute[x]).

    • Example: For filename stage.txt, give readable, writable, and executable permission for the owner, type chmod 700 stage.txt or chmod u+rwx stage.txtor chmod u=rwx stage.txt and press Enter.

    •             ubuntu@ip-172-31-38-2:~$ ls -l
                  total 0
                  ----rw-r-- 1 ubuntu ubuntu 0 Oct 26 09:25 stage.txt
                  ubuntu@ip-172-31-38-2:~$ chmod 700 stage.txt
                  ubuntu@ip-172-31-38-2:~$ ls -l
                  total 0
                  -rwx------ 1 ubuntu ubuntu 0 Oct 26 09:25 stage.txt
      
    • "u" is for the User (Owner), controlling the owner's permissions.

    • "g" is for the Group, managing permissions for users in the same group as the file.

    • "o" is for Others, determining permissions for all remaining users on the system who are neither the owner nor in the group.

    • "a" stands for "all" or "everyone," and it represents a setting that applies to the owner, group, and others collectively.

NumberOctal Permission RepresentationReference
0No permission---
1Execute permission--x
2Write permission-w-
3Execute and write permission: 1 (execute) + 2 (write) = 3-wx
4Read permissionr--
5Read and execute permission: 4 (read) + 1 (execute) = 5r-x
6Read and write permission: 4 (read) + 2 (write) = 6rw-
7All permissions: 4 (read) + 2 (write) + 1 (execute) = 7rwx
  1. To check which commands you have run till now.

    • Use the history command.

    •             ubuntu@ip-172-31-38-2:~$ history
                      1  ls
                      2  cd /home
                      3  tree
                      4  sudo apt install tree
      
  2. To remove a directory/ Folder.

    • Use the rmdir command to remove an empty directory or rm -r for non-empty directories/files.

    • Example: To remove a directory named ops, use rmdir ops or rm -r ops or rm -rf ops

    •             ubuntu@ip-172-31-38-2:~$ ls
                  ops  stage.txt
                  ubuntu@ip-172-31-38-2:~$ rmdir ops
                  ubuntu@ip-172-31-38-2:~$ ls
                  stage.txt
      
  3. To create a fruits.txt file and to view the content.

    • Use the touch command to create an empty file, and cat to view its content.

    • use echo command to create and add contents to a file in a single line.

    • use | symbol to merge two commands in a single line.

    •             ubuntu@ip-172-31-38-2:~$ echo "apple">fruits.txt| cat fruits.txt
                  apple
      
  4. Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.

    • Use the echo command and the "-e" option with "\n" for a new line.

    •             ubuntu@ip-172-31-38-2:~$ echo -e "Apple\nMango\nBanana\nCherry\nKiwi\nOrange\nGuava" > devops.txt
                  ubuntu@ip-172-31-38-2:~$ cat devops.txt
                  Apple
                  Mango
                  Banana
                  Cherry
                  Kiwi
                  Orange
                  Guava
      
  5. To Show only the top three fruits from the file.

    • Use the head command with parameter value 3.

    •             ubuntu@ip-172-31-38-2:~$ head -n 3 devops.txt
                  Apple
                  Mango
                  Banana
      
  6. To Show only the bottom three fruits from the file.

    • Use the tail command with parameter value 3.

    •             ubuntu@ip-172-31-38-2:~$ tail -n 3 devops.txt
                  Kiwi
                  Orange
                  Guava
      
  7. To create another file Colors.txt and to view the content.

    • Use the touch command to create an empty file, and cat to view its content.

    •             ubuntu@ip-172-31-38-2:~$ touch colors.txt | cat colors.txt
      
  8. Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, and Grey.

    • Use the echo command and the "-e" option with "\n" for a new line.

    •             ubuntu@ip-172-31-38-2:~$ echo -e "Red\nPink\nWhite\nBlack\nBlue\nOrange\nPurple\nGrey" > colors.txt
                  ubuntu@ip-172-31-38-2:~$ cat colors.txt
                  Red
                  Pink
                  White
                  Black
                  Blue
                  Orange
                  Purple
                  Grey
      
  9. To find the difference between fruits.txt and Colors.txt file.

  • Use the diff command.

  • Example: To find the difference between fruits.txt and Colors.txt, use diff fruits.txt Colors.txt.

  •             ubuntu@ip-172-31-38-2:~$ cat colors.txt
                Red
                Pink
                White
                Black
                Blue
                Orange
                Purple
                Grey
                ubuntu@ip-172-31-38-2:~$ cat fruits.txt
                Red
                apple
                Mango
                banana
                cherry
                kiwi
                Purple
                ubuntu@ip-172-31-38-2:~$ diff colors.txt fruits.txt
                2,6c2,6
                < Pink
                < White
                < Black
                < Blue
                < Orange
                ---
                > apple
                > Mango
                > banana
                > cherry
                > kiwi
                8d7
                < Grey
    
  • In the above output:

    The diff colors.txt fruits.txt command is used to find the differences between the "colors.txt" and "fruits.txt" files. It shows the lines that are unique to each file and the lines that differ between them.

    • The lines marked with < are unique to "colors.txt" (e.g., Pink, White, Black, Blue, Orange).

    • The lines marked with > are unique to "fruits.txt" (e.g., apple, Mango, banana, cherry, kiwi).

    • The line marked with < Grey indicates that "Grey" is in "colors.txt" but not in "fruits.txt."

    • The line 8d7 indicates that line 8 (Grey) is deleted in "fruits.txt," which is not present in "colors.txt."

Additional commands🧨

  1. View All Groups:

    To see a list of all groups, use getent group.

  2. View Members of a Specific Group:

    To check the members of a specific group, use getent group groupname.

  3. View User Accounts:

    To display a list of all user accounts, both regular and system accounts, use cut -d: -f1 /etc/passwd.

  4. View User and Group Ownership of Files:

    To view user and group ownership of files in a directory, you can use the ls -l command, which provides detailed information about files, including ownership, permissions, and more. For example:

    •             ubuntu@ip-172-31-38-2:~$ ls -l
                  total 4
                  -rw-rw-r-- 1 ubuntu ubuntu 42 Oct 26 10:29 fruits.txt
      
      • File Type: It's a regular file, indicated by a dash. If it were a directory, it would be denoted as "d" instead of a dash.

      • Owner (ubuntu): The owner of the file, which is the user "ubuntu," has read and write permissions but lacks execute permission. This means the owner can view and modify the file's content but cannot execute it as a script or program.

      • Group (ubuntu): Members of the group "ubuntu," which includes the owner and others, also have read and write permissions but no execute permission.

      • Others: All users who are neither the owner nor part of the group have read-only permissions. They can view the file's content but cannot modify or execute it.

      • Hard Links: The number "1" indicates the count of hard links to the file. A hard link is a reference to the same file on the filesystem. When it's "1," it means there's only one reference to this file.

      • File Size: The file size is 10 bytes, which represents the amount of disk space the file occupies.

      • Last Modified: The date and time "Oct 25 12:08" show when the file was last modified, providing information about its update or creation time.

"Linux is not for everyone. It's only for people who want to know how their computer works"😊

More from this blog

Untitled Publication

12 posts