DEV Community

Cover image for Commands for Viewing and Sorting Files
yash sugandh
yash sugandh

Posted on

Commands for Viewing and Sorting Files

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

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

Similarly, we can view the 2nd file

grocery-2

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

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

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

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

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

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

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

rev-command

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

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

less commands

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

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

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

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

sort-basic

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

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

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

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

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

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.

Top comments (0)