Let's directly jump into the commands by doing some tasks that will cover some major Linux basic commands.
1️⃣Task 1
To view what's written in a file🔎
Let's say I have a file called linux-basic.txt and in this text-based file there is some content written on it. And you want to see the contents.
Then there is a command for that called cat
Cat is used to concatenate and display files on the terminal.
cat -n: This adds line numbers to all lines
cat –E: This shows $ at the end of the line
2️⃣Task 2
To change the access permissions of files🔐
There will be a case where you don't want others to modify your file or vice-versa. To change the permissions of files, there is a command called chmod
chmod 755 linux-basic.txt
This sets the permissions of linux-basic.txt
to read, write, and execute for the owner, and read and execute for the group and others.
The next three characters,
rwx
, indicate the permissions for the file owner (ubuntu
in this case). It means the owner has read (r
), write (w
), and execute (x
) permissions.The following three characters,
r-x
, indicate the permissions for the group (ubuntu
in this case). It means the group hasread
andexecute
permissions but does not havewrite
permission.The last three characters,
r-x
, represent the permissions for others (users not in the owner or group). They haveread
andexecute
permissions but nowrite
permission.
You can manage the accessibility according to you, like above.
3️⃣Task 3
To check which commands you have run till now🗒️
To perform the above task, there is a command called history
, where you can see all the commands you have used till now.
4️⃣Task 4
To remove a directory/ Folder📤
To remove a file, there is a command called rm
ie remove. But we cannot remove a folder by running just rm cmd. There is another cmd to remove a folder that is called rm -rf
The rm -rf
command is a more powerful variation that forcefully removes files and directories recursively without prompting for confirmation.
-r
(or--recursive
): This option enables the removal of directories and their contents recursively.-f
(or--force
): This option overrides any prompts or warnings and forcefully removes files and directories without asking for confirmation.
5️⃣Task 5
To create a fruits.txt file and to view the content🧺
Create fruits.txt file - To perform this, there are two ways you can consider.
Run touch cmd, this will create a file with no content. Then run
vi/vim
cmd, this will open up a text editor where you can write whatever you want by changing its mode to insert(i) and thenesc
>:wq!
to save and exit from the editor.Or you can run directly
vi/vim fruits.txt
cmd, this creates a file and open-up the editor directly.$vi fruits.txt This file is created to list my favourite fruit ie MANGO. -- -- :wq!
To view the content of the file - Run cat cmd as we have discussed earlier.
$ cat fruits.txt This file is created to list my favourite fruit ie MANGO.
6️⃣Task 6
Add content in devops.txt (One in each line) - Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava🥗
To perform the above task, we have to create a file called devops.txt and then add on some given contents. Let's do that!
Again, there are two ways (some more ways, comments :)
Method 1: Using a text editor
Open the
devops.txt
file in a text editor (such as Nano or Vim).Add the fruits manually, each on a new line.
Save the file and exit the text editor.
Methos 2: By using echo cmd
echo -e "Apple\nMango\nBanana\nCherry\nKiwi\nOrange\nGuava" >> devops.txt
This command appends the fruits to the file, with each fruit on a new line.
echo
: Theecho
command is used to display text or values on the terminal.-e
: This option is specific to theecho
command and enables the interpretation of backslash escapes. In this case, it allows the interpretation of\n
as a newline character."Apple\nMango\nBanana\nCherry\nKiwi\nOrange\nGuava"
: This is the string of text to be echoed. It consists of multiple fruits separated by newline characters (\n
), ensuring each fruit is on a new line.>>
: The double greater-than symbol (>>
) is a redirection operator in the shell. It appends the output of the command to the specified file instead of displaying it on the terminal.devops.txt
: This is the filename of the file where the output of theecho
command will be appended. In this case, it isdevops.txt
.
7️⃣Task 7
Show only the top three fruits from the file🥭
To perform the above task we have,
head -n 3 devops.txt
This command displays the first three lines (fruits) from the file.
head
: Thehead
command is used to display the first few lines of a file or input stream. By default, it displays the first 10 lines of a file.-n
: The-n
option is specific to thehead
command and is used to specify the number of lines to be displayed.3
: In this case,3
is the argument provided after the-n
option. It specifies that we want to display the first three lines of the file.
8️⃣Task 8
Show only the bottom three fruits from the file🥝
This is similar to the above task-7. Predictable, right?
tail -n 3 devops.txt
This command displays the last three lines (fruits) from the file.
9️⃣Task 9
To create another file Colors.txt and to view the content🧮
This is also what we have done above so I am not explaining this.
$vi Colors.txt
This file is created to list my fav colors ie Yellow & Blue.
--
--
:wq!
$ cat Colors.txt
This file is created to list my fav colors ie Yellow & Blue.
1️⃣0️⃣Task 10
Add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey🎨
Again, you know this.
echo -e "Red\nPink\nWhite\nBlack\nBlue\nOrange\nPurple\nGrey >> Colors.txt
1️⃣1️⃣Task 11
To find the difference between fruits.txt and Colors.txt files☯️
To perform the above task, there is a command called diff to see the difference between two files.
$diff fruits.txt Colors.txt
To see a side-by-side comparison, use the
-y
option:$diff -y fruits.txt Colors.txt
To see a context-based comparison, you use the
-c
option:$diff -c fruits.txt Colors.txt
To generate a unified diff, you use the
-u
option:$diff -u fruits.txt Colors.txt
Thank you!🖤