The Linux Commands Series: Part III

Ghost Rider
5 min readNov 28, 2021

tail, wc, grep, sort, uniq, diff, echo, chown, chmod, umask

developer.ibm.com

I intend to write a series of total VI blog posts describing the most used Linux Commands. This blog is the first of the series. I hope you all enjoy the reading.

Citation/References/Credit will be mentioned at the end of the post.

tail

Tail command with -f option will open a file at the end and watches for change in content. Whenever there is new content in the file, it would print in the window.

tail -f <filename>

use Ctrl+C to exit.

To view the last 5 lines in a file:

tail -n 5 <filename>

To view the content starting from line 5:

tail -n +5 <filename>

To view the last 5 lines every time the file gets updated:

tail -5f <filename>

wc

WC is used to count the words in a file.

wc <filename> or wc file1.txt

result: a b c

Column a represents the number of lines, column b represents the number of words and c represents the number of bytes.

Just to count the number of lines:

wc -l file1.txt

Just to count the number of words:

wc -w file1.txt

Just to count the bytes:

wc -c file1.txt or wc -m file1.txt

To use with pipes, for example, to count the output of running the command, ls -al:

ls -al|wc

grep

Grep stands for global regular expression print. It’s used to find and print the line containing a particular search pattern in a file. Example:

grep <search pattern> <filename>

grep Alex file1.txt

For line number use -n and -i for case insensitive search:

grep -ni <search pattern> <filename>

Grep can also be used to filter on an output of another command, for example:

less <filename> | grep -n <search pattern>

To invert the result, i.e., to exclude the lines matching a particular pattern use -v option:

ls -la | grep -v Alex.txt

sort

Imagine you have a file with vegetable names and you want to sort them alphabetically, then use sort:

sort vegetable.txt

Use r to sort in reverse order:

sort -r vegetable.txt

Sorting is case sensitive, so use — ignore-case option to sort case-insensitively. You can also use the -n option based on numerical order.

If the file contains duplicates then use u to remove them.

sort -u vegetable.txt

Sort can be used with pipes to work on the output of a different command:

ls -la| sort

uniq

Uniq command is similar to sort but it removes the adjacent duplicate lines.

We can use uniq in combination with other commands:

ls -la | uniq

sort file.txt | uniq

You can display duplicate lines using the -d option:

sort file.txt | uniq -d

You can display non-duplicate lines using -u option:

sort file.txt | uniq -u

You can count occurrences of each line using -c option:

sort file.txt | uniq -c

Now, based on the above command, to sort based on the highest frequency of lines:

sort file.txt | uniq -c | sort -nr

diff

Diff command is used to find the difference between the files, for example:

diff file1.txt file2.txt

The output will be the content which is differing in the files.

To compare file line by line then use -y option:

diff -y file1.txt file2.txt

You can compare directories too but use -r to compare recursively:

diff -r dir1 dir2

To view the files which differ rather than the content then use options r and q:

diff -rq dir1 dir2

echo

Echo prints the argument on the terminal, for example, to print Hola on terminal:

echo “Hola”

To append the output to a file:

echo “Hola” >> file.txt

You need to provide backslash to a special character in order to print it:

echo I have \$10

To populate the variables, use quotes. For example to interpret a variable Amount:

echo “I have $Amount”

To echo files in the current folder use *:

echo *

To echo files starting with En:

echo En*

To generate ranges:

echo {1..9}

chown

Chown is change ownership. It is used to change ownership of a file or a directory to another user. Ownership can be changed by only the owner of the file.

If I am the owner of a file file1.txt and to change its ownership to a user then:

chown <user> <file>

To change ownership of a directory and subdirectories and its files, use -R, to do it recursively:

chown -R <owner> <dir>

To change group ownership along with user ownership:

chown <user>:<group> <file>

You can also use chgrp to change the group of a file:

chgrp <group> <filename>

chmod

Chmod is used to change the file permissions. For example, when we execute ls -al then the files in a folder will be listed along with the file permissions:

Example: drwxrwxrwx ….

The first letter indicates that it's a directory. Similarly, ‘-’ indicates a file and ‘l’ indicates a link.

First ‘rwx’ indicates the user, having ‘read’, ‘write’ and ‘execute’ permissions. Second ‘rwx’ indicates the group, having ‘read’, ‘write’ and ‘execute’ permissions. Third ‘rwx’ indicates everyone else, having ‘read’, ‘write’ and ‘execute’ permissions.

Numerically assigning permissions is very easy,

1 -> has execution permission (001 = x)

2 -> has write permission (010 = w)

4 -> has read permission (100 = r)

This gives us several combinations:

0 -> no permissions (000 = — -)

1 ->has execution permission (001 = x)

2 ->has write permission (010 = w)

3 -> write, execute (011)

4 ->read (100)

5 ->read, execute(101)

6 ->read, write(110)

7 ->read, write, execute(111)

Syntax:

chmod 777 <filename>

chmod 700 <filename>

umask

Umask along with option -S helps us to read default permissions in the system that would be assigned to a file or a directory or a link.

umask -S

Example output: u=rwx,g=rx,o=rx

Note: Remember to use the man command for more intel on a particular command.

This is the end of this blog post. I will publish a blog post in this series once a week.

Citation/Reference: flaviocopes.com

Please follow me for more interesting content.

--

--

Ghost Rider

I am a geek and technologist, at-least that’s what I would like call myself. I love anonymity. Let’s rock and roll!