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
- du: Linux Command ‘du’ estimates file space usages.
- -a: Flag ‘-a’ with the command ‘du’ write counts for all files.
- /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.
- |: The pipeline operator.
- sort: As the name suggests, Linux command ‘sort’, can be used to sort lines of text files.
- -n: Flag ‘-n’ with the command ‘sort’ compare according to string numerical values.
- -r: Flag ‘-r’ with the command ‘sort’ reverse the results of the comparisons.
- |: Again a pipeline operator.
- head: Linux Command ‘head’ output the first part of file.
- -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
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
Description of the above one-liner script
In addition to the above detailed description,
- -h: Flag ‘-h’ with the command ‘du’ print sizes in human readable format.
- -s: Flag ‘-s’ with the command ‘du’ summarizes the output by displaying a total of each argument, only.
- -x: Flag ‘-x’ with the command ‘du’ skips directories on different file systems.
- -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
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.