Commands for Viewing and Sorting Files
Tags: beginners, linux, ubuntu, computerscience
Published: 2020-07-06 07:57:40

In the last few posts, we looked at I/O Redirection and Piping in Linux System.

Today we are going to look at commands that help us in viewing and sorting files.

Let's start from the command that we have used before multiple times - `cat` command.

The `cat` command is used to concat multiple files together or just read the files.

**1.`cat` command**

The syntax for cat command

![cat-syntax](https://dev-to-uploads.s3.amazonaws.com/i/wtwdkbdr9o4t64enh4ff.png)

Let's take an example where we have two grocery lists grocery1.txt and grocery2.txt

First, let's view the file content using cat

![grocery-cat](https://dev-to-uploads.s3.amazonaws.com/i/1qbuqbz0h3eyknjjhmjo.png)

Similarly, we can view the 2nd file

![grocery-2](https://dev-to-uploads.s3.amazonaws.com/i/gq3tyuijyzyc3goxpvhu.png)

In the above examples, we used the command `cat <filename>` to view the file contents.

What if we wanted to concat these 2 files?

- concat above files

![cat-concat](https://dev-to-uploads.s3.amazonaws.com/i/9ava5iyz13l28cpgk8rv.png)

In the above example, we used the command `cat grocery1.txt grocery2.txt > groceryFinal.txt` where

`cat` represents the concat command
`grocery1.txt grocery2.txt` represents **files to concat**.
`>` represents redirection of **Output Stream**
`groceryFinal.txt` represents the **output file**.

Then to confirm that all the content is present we used `cat groceryFinal.txt` to view the content of the output file.

But the above output is a little hard to read, what if we wanted to view the output file but numbered?

Let's check it out:-

- View the output file numbered

![grocery-final](https://dev-to-uploads.s3.amazonaws.com/i/gg1ual1de2sg2ut8h3gg.png)

In the above example, we used the command `cat -n groceryFinal.txt` where

`cat` represents the **concat** command
`-n` represents the option to **number all output line**
`groceryFinal.txt` represents the **file** to view the content of

**2. `tac` command**

The `tac` command which is just the reverse of cat command starts reading from bottom.

It does not change the content it just reads from the bottom.

![tac-syntax](https://dev-to-uploads.s3.amazonaws.com/i/qrlv706gteh9fyzca6ww.png)

To use `tac` command we simply use `tac` along with the "filename"

Let's just use the previously concatenated file as input for the `tac` command.

![tac-command-output](https://dev-to-uploads.s3.amazonaws.com/i/3395tuog2hp4mm90ga62.png)

In the above example, we used the command `tac groceryFinal.txt` which printed the content of the file from bottom up.

**3. `rev` command**

The `rev` command is used to reverse the content of the file.

It reads the file from the top but the content will be reversed.

The syntax for `rev` command

![rev-syntax](https://dev-to-uploads.s3.amazonaws.com/i/crt05jpubes1zlhz5jub.png)

To use `rev` command we simply use `rev` along with the "filename"

Let's again use our **groceryFinal.txt** as an input

![rev-command](https://dev-to-uploads.s3.amazonaws.com/i/fojkxtgfct2ucnqvhiqh.png)

In the above example, we used the command `rev groceryFinal.txt` which printed the content of the file reversed.

I can't seem to find the practical usage of `rev` command other than irritating someone by sending them the file after applying `rev` command :)

**4. less command**

The `less` command provides us a way to view text files in our terminal itself.

The syntax for opening a file using `less` command

![less-syntax](https://dev-to-uploads.s3.amazonaws.com/i/22pi8d6tifritm0p2rlq.png)

To open a file using `less` we only need to call `less` command along with the filename.

![less commands](https://dev-to-uploads.s3.amazonaws.com/i/hbquo8yy0ihb5vpdsfq9.png)

**less** provides us tons of functionality such as search a character, scroll up/down, etc.

**5. Head and Tail**

`head` command is used to print the lines from the start of the file whereas the `tail` command is used to print the files from the bottom of the file.

Let's take an example for better understanding

- print only the first two line from a file

![head-example](https://dev-to-uploads.s3.amazonaws.com/i/7ux4q9vgcbb07r3qceit.png)

In the above example, we used the command `head -n 2 groceryFinal.txt` where

`head` represents the **head** command
`-n 2` represents the **number of lines** option
`groceryFinal.txt` represents the **file we want to read**

- print only the last two lines from a file

![tail-example-updated](https://dev-to-uploads.s3.amazonaws.com/i/uhd0lyhd5r6az5egnx75.png)

`tail` represents the **tail** command
`-n 2` represents the **number of lines** option
`groceryFinal.txt` represents the **file we want to read**

So `head` and the `tail` command is basically used to view the snippet of file content.

**6. `sort` command** 

The `sort` command is used to sort lines of text files.

The syntax for `sort` command

![sort-syntax](https://dev-to-uploads.s3.amazonaws.com/i/bmrdvlt2ilgiatk4egsk.png)

To use `sort` command we simply use `sort` command along with `options` and `filename`

![sort-basic](https://dev-to-uploads.s3.amazonaws.com/i/dj3j13184rsbpw2soihe.png)

In the above example, we use the command `sort groceryFinal.txt` which sorts the list in ascending order.

What if we want to `sort` in **descending order**?

![sort-reversed](https://dev-to-uploads.s3.amazonaws.com/i/y6pmvbunbw06hm8ce417.png)

In the above example, we used the command `sort -r groceryFinal.txt` where

`sort` represents **sort** command
`-r` represents the **reverse** option
`groceryFinal.txt` represents **the input file**

Can you guess what else can we use to get the same kind of output?

`sort groceryFinal.txt | tac`

If you guessed the `tac` command you were right. We can use the `tac` command along with `sort` command convert the **ascending** sorted output to **descending** output.

But what about numbers?

Let's see what happens when we use the `sort` command with numbers

![sort-example-numbers](https://dev-to-uploads.s3.amazonaws.com/i/68si8lhbbizim0r81fis.png)

Wait, that seems wrong but why?

So the `sort` command by default compares digit by digit.

So what can we do for sorting numbers?

- sort file with respect to numbers

![sort-number-example](https://dev-to-uploads.s3.amazonaws.com/i/btgvw8c2dzspo6vc67de.png)

In the above example, we used the command `sort -n numbers.txt` where
`sort` represents **sort** command
`-n` represents **numeric-sort**
`numbers.txt` represents **file-to-be-sorted**

- sort and only return distinct values

 ![sort-unique](https://dev-to-uploads.s3.amazonaws.com/i/qcswxs6378ecvh2vfmfl.png)

In the above example, we used the command `sort -un numbers.txt` where
`sort` represents **sort** command
`-un` represents **unique numeric-sort**
`numbers.txt` represents **file-to-be-sorted**

What if we have data with multiple columns and we want to sort data on the basis of a specific column?

- sort data based on column 2

![sort-cplumn](https://dev-to-uploads.s3.amazonaws.com/i/uoye03i1urrrb1ly0upe.png)

In the above example, we used the command `sort -k 2n employees.txt` where

In the above example, we used the command `sort -un numbers.txt` where
`sort` represents **sort** command
`-k 2n` represents **key i.e. column 2 numeric-sort**
`employees.txt` represents **file-to-be-sorted**

This was all about Viewing and Sorting Files in the Linux System. 

Please, let me know if there are any questions and suggestions on what you want us to explore next.