Day 3 : Essential Linux Commands

Shahanaz ShanuShahanaz Shanu
4 min read

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

ubuntu@ip-172-31-90-1:~/shanu$ cat test-2.txt
This is a new file

Cat: This is used to concatenate and display files on the terminal. It can also be used to modify existing ones.

TASK 2: To change the access permissions of files.

ubuntu@ip-172-31-90-1:~/shanu$ ll test-2.txt
-rw-rw-r-- 1 ubuntu ubuntu 19 Jul 20 19:14 test-2.txt
ubuntu@ip-172-31-90-1:~/shanu$ chmod 644 test-2.txt
ubuntu@ip-172-31-90-1:~/shanu$ ll test-2.txt
-rw-r--r-- 1 ubuntu ubuntu 19 Jul 20 19:14 test-2.txt

Chmod: This command is used to change the access permissions of files and directories. In numeric notation, each permission is represented by a three-digit octal number. Each digit corresponds to a category (user, group, others) and represents a combination of read (4), write (2), and execute (1) permissions.Three types of permissions can be set for a file or directory:

  1. Read (r): Allows a user to read the content of a file or view the names of files within a directory.

  2. Write (w): Permits a user to modify the contents of a file or create, delete, and rename files within a directory.

  3. Execute (x): Gives a user the ability to execute a file (if it's a program or script) or traverse a directory (if it's a directory).

TASK 3: To check which commands you have run till now.

ubuntu@ip-172-31-90-1:~/shanu$ history
    1  pwd
    2  ll
    3  cd shanu
    4  ll
    5  echo "This is the text box" > test-1.txt
    6  echo "This is a new file" > test-2.txt
    7  ll
    8  cat test-2.txt
    9  LL
   10  ll
   11  ll test-2.txt
   12  chmod 644 test-2.txt
   13  ll
   14  ll test-2.txt
   15  history

TASK 4 : To remove a directory/ Folder.

ubuntu@ip-172-31-90-1:~/shanu$ rm -r test
ubuntu@ip-172-31-90-1:~/shanu$ ll
total 20
drwxrwxr-x 3 ubuntu ubuntu 4096 Jul 20 21:02 ./
drwxr-x--- 5 ubuntu ubuntu 4096 Jul 19 14:57 ../
drwxrwxr-x 3 ubuntu ubuntu 4096 Jul 19 15:15 A/
-rw-rw-r-- 1 ubuntu ubuntu   21 Jul 20 19:14 test-1.txt
-rw-r--r-- 1 ubuntu ubuntu   19 Jul 20 19:14 test-2.txt
-rw-rw-r-- 1 ubuntu ubuntu    0 Jul 19 15:03 test-3.txt
-rw-rw-r-- 1 ubuntu ubuntu    0 Jul 19 15:03 test-4.txt
-rw-rw-r-- 1 ubuntu ubuntu    0 Jul 19 15:03 test-5.txt
-rw-rw-r-- 1 ubuntu ubuntu    0 Jul 19 15:03 test-6.txt

rm: This command is used to delete files, but it does not work for directories unless you use the -r option.

-r : stands for "recursive," and it is used to delete directories and their contents, including subdirectories and files

TASK 5: To create a fruits.txt file and view the content.

ubuntu@ip-172-31-90-1:~/shanu/A$ touch fruits.txt
ubuntu@ip-172-31-90-1:~/shanu/A$ echo -e "Apple ,\nOrange ,\nGrapes,\nMango" > fruits.txt
ubuntu@ip-172-31-90-1:~/shanu/A$ cat fruits.txt
Apple ,
Orange ,
Grapes,
Mango

touch : command is used to create new, empty files

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

ubuntu@ip-172-31-90-1:~/shanu/A$ touch devops.txt 
ubuntu@ip-172-31-90-1:~/shanu/A$ echo -e "Apple,\nMango,\nBanana,\nCherry,\nKiwi,\nOrange,\nGuava." > devops.txt
ubuntu@ip-172-31-90-1:~/shanu/A$ cat devops.txt
Apple,
Mango,
Banana,
Cherry,
Kiwi,
Orange,
Guava.

echo : This command is used to display messages.

-e : This option is used with echo, enables the interpretation of escape sequences in the specified string. Escape sequences are special character sequences that begin with a backslash \ and are used to represent non-printable characters or to add formatting to the output. eg: \n used for new line

TASK 7: To Show only the top three fruits from the file.

ubuntu@ip-172-31-90-1:~/shanu/A$ head -3 devops.txt
Apple,
Mango,
Banana,

head : It is used to displays the first 10 lines of a file, but you can specify a different number of lines using options.

TASK 8: To Show only the bottom three fruits from the file.

ubuntu@ip-172-31-90-1:~/shanu/A$ tail -3 devops.txt
Kiwi,
Orange,
Guava.

Tail: This command prints the last N number of data of the given input. By default, it prints 10 lines.

We can specify the number of lines, that we want to display.

TASK 9 : To create another file Colors.txt and to view the content.

ubuntu@ip-172-31-90-1:~/shanu/A$ touch colors.txt
ubuntu@ip-172-31-90-1:~/shanu/A$ echo -e "Blue ,\nGreen ,\nRed ,\nOrange ,\nYellow ,\nViolet" > colors.txt
ubuntu@ip-172-31-90-1:~/shanu/A$ cat colors.txt
Blue ,
Green ,
Red ,
Orange ,
Yellow ,
Violet

TASK 10: Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.

ubuntu@ip-172-31-90-1:~/shanu/A$ touch colors.txt
ubuntu@ip-172-31-90-1:~/shanu/A$ echo -e "Red,\nPink,\nWhite,\nBlack,\nBlue,\nOrange,\nPurple,\nGrey." > colors.txt
ubuntu@ip-172-31-90-1:~/shanu/A$ cat colors.txt
Red,
Pink,
White,
Black,
Blue,
Orange,
Purple,
Grey.

TASK 11: To find the difference between fruits.txt and Colors.txt file.

ubuntu@ip-172-31-90-1:~/shanu/A$ diff fruits.txt colors.txt 
1,4c1,8
< Apple ,
< Orange ,
< Grapes,
< Mango
---
> Red,
> Pink,
> White,
> Black,
> Blue,
> Orange,
> Purple,
> Grey.

diff : It is used to compare and display the differences between two text files. It is particularly useful for identifying changes made to files, such as configuration files, source code files, or any other plain text files.

🎉Conclusion

Thank you for taking the time to read!🙏 Drop me your feedback/comments. Feel free to share this article with others if you like it.

0
Subscribe to my newsletter

Read articles from Shahanaz Shanu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shahanaz Shanu
Shahanaz Shanu