Linux Commands with practical examples

Linux Commands with practical examples

·

4 min read

Command to view what's written inside a file?

The "cat" command shows the content written inside the file.

cat <filename>

Example:

Suppose, we have one file named "file1.txt" and inside this file "Hello DevOps" is written. So, to view what is written inside the file you can type the following command in the terminal:


Command to change the access permission of a file?

The "chmod" command gives all permissions i.e. read, write and execute permissions to all ( Users, Groups and Others ).

chmod 777 file1.txt

In the above screenshot, file1.txt has all the permissions ( Read, Write, Execute )

Let's Understand how to provide permissions to a file in detail.

OctalFile mode
1--x, executable permission
2--w, write permission
3--r, read permission

Suppose if we want to provide executable permission to groups, write permission to users and read permission to others, then the command should be like this

executable permission to groups i.e. octal "1"

write permission to users i.e. octal "2"

read permission to others i.e. octal "4"

command: chmod 124 file1.txt


Command to check which commands you have run till now.

"history" command shows us what all commands we have you till now.

history


Command to remove Directory/Folder in Linux?

To remove a directory or folder in Linux, you can use the "rmdir" or "rm" command.

To remove an empty directory

rmdir <directory-name>

To remove non-empty directories

rm -r <directory-name>

Here, -r means recursively.


Command to create a fruits.txt file and to view the content?

To create a file "touch" command is used

touch <filename>

To view the content of a file, the "cat" command is used

cat <filename>


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

First create a file with "touch" command

Example: touch devops.txt

Open the devops.txt file with vim editor

vim, nano editors are used to add content to a file.

Example: vim devops.txt

Press "i" to go into insert mode and add contents to the file

and press "Esc + :wq" to save and exit from the file.


Command to show only top three fruits from the file?

The "head" command gives us the first part of a file.

head -n <filename>

"-n" option to specify the number of lines to display


Command to Show only bottom three fruits from the file.

The "tail" command gives us last part of a file.

tail -n <filename>

"-n" to specify the number of lines to display from the end of the file.


Command to create another file Colors.txt and to view the content.

To create the file "touch" command is used.

touch <filename>

To see the content of a file

cat <filename>


Command to add content in Colors.txt (One in each line) - Red, Pink, White, Black, Blue, Orange, Purple, Grey.

Open colors.txt file with vim editor

vim, nano editors are used to add content to a file.

Example: vim colors.txt

Press "i" to go into insert mode and add contents to the file

and press "Esc + :wq" to save and exit from the file.


Command to find the difference between fruits.txt and Colors.txt files.

**"diff" command is used to check the difference between two files.

diff <file1> <file2>

Let's understand the difference

Here in devops.txt we have content

Apple
red
blue
yellow
green
maroon

and in "colors.txt" file we have

red
blue
yellow
green
maroon

Each line will be prefixed with "<" or ">" to indicate which file it belongs to.

Happy Learning!!

Thanks For Reading! :)