Day2: Basic of Linux Commands(Part-2)

sonu mishrasonu mishra
4 min read

• Linux command to view what’s written in a file:

1. Cat
This is the simplest and perhaps the most popular command to view a file in Linux.

2. nl
The nl command is almost like the cat command. The only difference is that it prepends line numbers while displaying the text in the terminal.

3. Less
Less command views the file one page at a time. The best thing is that you exit less (by pressing q), there are no lines displayed on the screen.

4. Head
Head command is another way of viewing text file but with a slight difference. The head command displays the first 10 lines of a text file by default.

5. Tail
Tail command in Linux is similar and yet opposite to the head command. While head command displays file from the beginning, the tail command displays file from the end.

• Linux command to change the access permissions of files:

  • There are three options for permission groups available to you in Linux. They are..

Owners:- these permissions will only apply to owners and will not affect other groups.
**
Groups:- you can assign a group of users specific permissions, which will only impact users within the group. * All users:-** these permissions will apply to all users, and as a result, they present the greatest security risk and should be assigned with caution.

  • There are three kinds of file permissions in Linux:-

Read (r): Allows a user or group to view a file.

Write (w): Permits the user to write or modify a file or directory.

Execute (x): A user or grup with execute permissions can execute a file or view a directory.

  • To change directory permissions in Linux, use the following:-

  • The command for changing directory mode permissions we used “chmod”.
    chmod +rwx filename to add permissions
    chmod -rwx directoryname to remove permissions.
    chmod +x filename to allow executable permissions.
    chmod -wx filename to take out write and executable permissions.
    Note that “r” is for read, “w” is for write, and “x” is for execute.
    This only changes the permissions for the owner of the file.

  • Changing Linux permissions in numeric code :-

⁃ You may need to know how to change permissions in numeric code in Linux, so to do this you use numbers instead of “r”, “w”, or “x”.
0 = No Permission
1 = Execute
2 \= Write
4 = Read

For example:-
• chmod 777 foldername will give read, write, and execute permissions for everyone.
• chmod 700 foldername will give read, write, and execute permissions for the user only.
• chmod 327 foldername will give write and execute (3) permission for the user, w (2) for the group, and read, write, and execute for the users.

• Linux command to create a fruits.txt file and to view the content:

⁃ So to create a fruits.txt file we are using echo command and then we use cat command to display the contents
⁃ Step1) Create Vim/Vi or nano fruit.txt
⁃ Step2) To View cat fruits.txt

• Linux command to add content in devops.txt (One in each line) — Apple, Mango, Banana, Cherry, Kiwi, Orange, Guava.

⁃ step1) create nano devops.txt
⁃ step2) To view cat devops.txt

• Linux command to show only the top three fruits from the file:

⁃ Step1)Create nano devops.txt (file)
⁃ Step2) Use head -n filename — (cmd)
Eg. head -3 devops.txt

• Show only the bottom three fruits from the file:

⁃ It is also called as tail commands.
⁃ Step1)Create nano devops.txt (file)
⁃ Step2) tail -3 devops.txt

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

⁃ Step1)Create nano Colour.txt (file)
⁃ step2)To view cat Colour.txt

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

⁃ To find the difference between the fruits.txt and Colors.txt files.

⁃ To differentiate two files like a and b then diff command is used.
⁃ Step1) diff devops.txt Colour.txt

3
Subscribe to my newsletter

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

Written by

sonu mishra
sonu mishra

AWS || git || Linux || networking || DevOps || Docker || Git || Kubernates || Terraform || Jenkins || Ansible || Jira || Prometheus || Grafana ||