Day 03 Task: Basic Linux Commands🚀🥇🔐✏

Task: What is the Linux command to
To view what's written in a file.
Use the
catcommand.Example: To view the contents of a file named
devops.txt, typecat devops.txtand press Enter.ubuntu@ip-172-31-38-2:~$ cat devops.txt hello welcome to the world of devops
To change the access permissions of files.
Use the
chmodcommand with permission parameters(read[r],write[w],execute[x]).Example: For filename
stage.txt, give readable, writable, and executable permission for the owner, typechmod 700 stage.txtorchmod u+rwx stage.txtorchmod u=rwx stage.txtand 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.
| Number | Octal Permission Representation | Reference |
| 0 | No permission | --- |
| 1 | Execute permission | --x |
| 2 | Write permission | -w- |
| 3 | Execute and write permission: 1 (execute) + 2 (write) = 3 | -wx |
| 4 | Read permission | r-- |
| 5 | Read and execute permission: 4 (read) + 1 (execute) = 5 | r-x |
| 6 | Read and write permission: 4 (read) + 2 (write) = 6 | rw- |
| 7 | All permissions: 4 (read) + 2 (write) + 1 (execute) = 7 | rwx |
To check which commands you have run till now.
Use the
historycommand.ubuntu@ip-172-31-38-2:~$ history 1 ls 2 cd /home 3 tree 4 sudo apt install tree
To remove a directory/ Folder.
Use the
rmdircommand to remove an empty directory orrm -rfor non-empty directories/files.Example: To remove a directory named
ops, usermdir opsorrm -r opsorrm -rf opsubuntu@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
To create a fruits.txt file and to view the content.
Use the
touchcommand to create an empty file, andcatto view its content.use
echocommand 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
Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.
Use the
echocommand 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
To Show only the top three fruits from the file.
Use the
headcommand with parameter value 3.ubuntu@ip-172-31-38-2:~$ head -n 3 devops.txt Apple Mango Banana
To Show only the bottom three fruits from the file.
Use the
tailcommand with parameter value 3.ubuntu@ip-172-31-38-2:~$ tail -n 3 devops.txt Kiwi Orange Guava
To create another file Colors.txt and to view the content.
Use the
touchcommand to create an empty file, andcatto view its content.ubuntu@ip-172-31-38-2:~$ touch colors.txt | cat colors.txt
Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, and Grey.
Use the
echocommand 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
To find the difference between fruits.txt and Colors.txt file.
Use the
diffcommand.Example: To find the difference between
fruits.txtandColors.txt, usediff 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 < GreyIn the above output:
The
diff colors.txt fruits.txtcommand 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
< Greyindicates that "Grey" is in "colors.txt" but not in "fruits.txt."The line
8d7indicates that line 8 (Grey) is deleted in "fruits.txt," which is not present in "colors.txt."
Additional commands🧨
View All Groups:
To see a list of all groups, use
getent group.View Members of a Specific Group:
To check the members of a specific group, use
getent group groupname.View User Accounts:
To display a list of all user accounts, both regular and system accounts, use
cut -d: -f1 /etc/passwd.View User and Group Ownership of Files:
To view user and group ownership of files in a directory, you can use the
ls -lcommand, 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.txtFile 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"😊




