Tips & Tricks: Find Largest File and Print the content of a file in reverse

We are pleased to announce Linux Tips and Tricks Series for our beloved readers. This series aims at giving you a quick yet detailed answer of your’s everyday queries.

Here in this article we will see How to print largest file and directories on your Linux machine. Also you will learn about printing the content of a file in reverse.

1. Find Largest Files and Directories in Linux

Whether your Linux system is running out of space or not, you always wanted to know largest files and directories on your box. Here is how to find the largest file/directory in Linux.

$ du -a /path/to/directory | sort -n -r | head -n 10
Description of Above one liner script
  1. du: Linux Command ‘du’ estimates file space usages.
  2. -a: Flag ‘-a’ with the command ‘du’ write counts for all files.
  3. /path/to/directory: It is the path to the directory, where we want to perform this operation. If you want to find the largest file, including system roots, you may use ‘/’ as path, but yes, you need need root/sudo access to perform this operation successfully. Else ‘du’ won’t be able to read a directory.
  4. |: The pipeline operator.
  5. sort: As the name suggests, Linux command ‘sort’, can be used to sort lines of text files.
  6. -n: Flag ‘-n’ with the command ‘sort’ compare according to string numerical values.
  7. -r: Flag ‘-r’ with the command ‘sort’ reverse the results of the comparisons.
  8. |: Again a pipeline operator.
  9. head: Linux Command ‘head’ output the first part of file.
  10. -n: Flag ‘-n’ with command head print the first ‘n’ number of files. The flag ‘-n’ always requires an argument. In the above example, we passed ’10’ as argument, to get top 10 results. You may adjust it as per your needs.

Find Top Ten Largest File/Directories in your home folder. Replace ‘avi’ with your home directory name or $user, in general, in the below example.

$ du -a /home/avi | sort -n -r | head -n 10
Sample Output


You may also, perform this operation at the system root, but only as a root user or with sudo.
Similarly, you may find top five Largest Files/Directories on your Linux box.

# du -a / | sort -n -r | head -n 5

To get the Output in human readable format, you may use the Linux command ‘du’ as described below.

Get a list of Top 10 Largest Directories in your home directory, in human readable format
$ du -hsx /home/avi/* | sort -rh | head -10
Sample Output


Description of the above one-liner script

In addition to the above detailed description,

  1. -h: Flag ‘-h’ with the command ‘du’ print sizes in human readable format.
  2. -s: Flag ‘-s’ with the command ‘du’ summarizes the output by displaying a total of each argument, only.
  3. -x: Flag ‘-x’ with the command ‘du’ skips directories on different file systems.
  4. -h: Flag ‘-h’ with the command ‘du’ compare human readable numbers.

Note: If you want non-summarized output, you may remove flag ‘-s’ from ‘du’ command in the above example.

2. Concatenate and print files in reverse

You must be aware of Linux Command ‘cat’ which concatenate files and print on the standard output. But do you know there is a command called tac (reverse of cat), in Linux, which concatenate and print files in reverse?

Before we proceed ahead, Lets create a file (let the name be tac_unixmen.txt) with the below contents (or your own content), to see the working of tac.

I am Number One.
2 comes at number two.
3 comes before number four.
4 is my lucky number.
First lets see what cat does with this file
$ cat tac_unixmen.txt


And then see what tac does
$ tac tac_unixmen.txt


You can also use –separator with Linux command ‘tac’ to use string as a separator instead of new line. Also You can use ‘tac’ to reverse a string directly from command-line.

Reverse the below string,using tac from the command-line. Use string separator ‘i’.

$ echo -n "UnixmenxisxthexbestxLinuxxCommunityx" | tac -s i
Sample Output


Lets look at the output closely,

The string contains following strings separated by i
‘Un’ i ‘xmenx’ i ‘sxthexbestxL’ i ‘nuxxCommun’ i ‘tyx’

The tac command prints these strings in the reverse order, as
‘tyx’ ‘nuxxCommun’ i ‘sxthexbestxL’ i ‘xmenx’ i ‘Un’ i

You may use the separator with ‘tac’ while concatenating files as well.

That’s all for now. I will be here again with another interesting article you will love to read. Till then stay healthy, tuned and connected to Unixmen. Don’t forget to give your valuable feedback in the comments below.

Like and share us and help us get spread.

  • Have you on any occasion generated large sums of money without reselling a thing and left out spending time on any of the referral marketing sort of task without the need to make an investment anything at all. I would like to tell you a work wherein just about all specialty tools is going to be provied to you as well as it is a very simple moreover trouble-free gig. Without having to start your domains and so on., almost all are going to be delivered to you free of cost. Folks will likely go to the landing page so you will get money for that. It is actually so simple as that. Now I am earning a ton of money from a home office using this work exactly like 20000 dollars regular monthly or more and if also you really want to receive same as that then follow here very simple info by visiting this web page >>>>> URL4.UK/AnyVl