Linux, FreeBSD, Juniper, Cisco / Network security articles and troubleshooting guides

FAQ
It is currently Thu May 06, 2021 8:27 pm


Username:
Subject:
Message body:
Enter your message here, it may contain no more than 60000 characters. 

Smilies
:D :) ;) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :!: :?: :idea: :arrow: :| :mrgreen: :geek: :ugeek:
Font size:
 
Font colour
Options:
BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON
Disable BBCode
Disable smilies
Do not automatically parse URLs
Confirmation code
Confirmation code:
In an effort to prevent automatic submissions, we require that you enter both of the words displayed into the text field underneath.
     

Topic review - How to list the largest files in a directory in Linux/FreeBSD
Author Message
Post subject: How to list the largest files in a directory in Linux/FreeBSD  |  Post Posted: Thu Jan 22, 2015 5:14 pm
One of the first things to do when cleaning up a filesystem is finding old, large and no longer necessary files.

Usually on Linux and Mac laptops, directories such as “Downloads” tend to become very large in time. Old installers and archives are the first ones at eating filesystem space.

How to list all files in current directory and sort them by their size.


For files it is enough to use the “ls” command. Either with “-h” option for human readable command or without.
Code:
$ ls -lah
total 430080
drwxr-xr-x  22 Andrei  staff   748B Jan 22 21:34 .
drwxr-xr-x+ 27 Andrei  staff   918B Jan 22 21:32 ..
-rw-r--r--   1 Andrei  staff   1.0M Jan 22 21:34 1
-rw-r--r--   1 Andrei  staff    10M Jan 22 21:34 10
-rw-r--r--   1 Andrei  staff    11M Jan 22 21:34 11
-rw-r--r--   1 Andrei  staff    12M Jan 22 21:34 12
-rw-r--r--   1 Andrei  staff    13M Jan 22 21:34 13
-rw-r--r--   1 Andrei  staff    14M Jan 22 21:34 14
-rw-r--r--   1 Andrei  staff    15M Jan 22 21:34 15
-rw-r--r--   1 Andrei  staff    16M Jan 22 21:34 16
-rw-r--r--   1 Andrei  staff    17M Jan 22 21:34 17
-rw-r--r--   1 Andrei  staff    18M Jan 22 21:34 18
-rw-r--r--   1 Andrei  staff    19M Jan 22 21:34 19
-rw-r--r--   1 Andrei  staff   2.0M Jan 22 21:34 2
-rw-r--r--   1 Andrei  staff    20M Jan 22 21:34 20
-rw-r--r--   1 Andrei  staff   3.0M Jan 22 21:34 3
-rw-r--r--   1 Andrei  staff   4.0M Jan 22 21:34 4
-rw-r--r--   1 Andrei  staff   5.0M Jan 22 21:34 5
-rw-r--r--   1 Andrei  staff   6.0M Jan 22 21:34 6
-rw-r--r--   1 Andrei  staff   7.0M Jan 22 21:34 7
-rw-r--r--   1 Andrei  staff   8.0M Jan 22 21:34 8
-rw-r--r--   1 Andrei  staff   9.0M Jan 22 21:34 9

So this shows files sizes in human readable formats: B, KB, MB, GB and so on if we’re lucky enough (not applicable to folders are indexed representations of files so they don’t actually eat filesystem space - more on that a bit later).

What if there are few tens or even hundreds of files, it will be difficult to go through all of them and remember which is the largest.

List files in directory ordered by their size ascendantly


Code:
$ ls -l | sort -n -k 5
total 430080
-rw-r--r--  1 Andrei  staff   1048576 Jan 22 21:34 1
-rw-r--r--  1 Andrei  staff   2097152 Jan 22 21:34 2
-rw-r--r--  1 Andrei  staff   3145728 Jan 22 21:34 3
-rw-r--r--  1 Andrei  staff   4194304 Jan 22 21:34 4
-rw-r--r--  1 Andrei  staff   5242880 Jan 22 21:34 5
-rw-r--r--  1 Andrei  staff   6291456 Jan 22 21:34 6
-rw-r--r--  1 Andrei  staff   7340032 Jan 22 21:34 7
-rw-r--r--  1 Andrei  staff   8388608 Jan 22 21:34 8
-rw-r--r--  1 Andrei  staff   9437184 Jan 22 21:34 9
-rw-r--r--  1 Andrei  staff  10485760 Jan 22 21:34 10
-rw-r--r--  1 Andrei  staff  11534336 Jan 22 21:34 11
-rw-r--r--  1 Andrei  staff  12582912 Jan 22 21:34 12
-rw-r--r--  1 Andrei  staff  13631488 Jan 22 21:34 13
-rw-r--r--  1 Andrei  staff  14680064 Jan 22 21:34 14
-rw-r--r--  1 Andrei  staff  15728640 Jan 22 21:34 15
-rw-r--r--  1 Andrei  staff  16777216 Jan 22 21:34 16
-rw-r--r--  1 Andrei  staff  17825792 Jan 22 21:34 17
-rw-r--r--  1 Andrei  staff  18874368 Jan 22 21:34 18
-rw-r--r--  1 Andrei  staff  19922944 Jan 22 21:34 19
-rw-r--r--  1 Andrei  staff  20971520 Jan 22 21:34 20

The “sort” command sorts the output by the 5th column (-k 5) numerically.

Listing directories and ordering by their sizes with above method is not going to work because directories do not hold data, they hold files, so they are actual index representations of filesystem blocks.
Example of a Linux system with a 4k block size:
Code:
$ sudo dumpe2fs -h /dev/disk/by-uuid/93a6ba6d-8750-4616-bf78-43e1b08c6595 | grep -i block
dumpe2fs 1.42.5 (29-Jul-2012)
Block count:              2499024
Reserved block count:     124951
Free blocks:              2032497
First block:              0
Block size:               4096
Reserved GDT blocks:      98user
Blocks per group:         32768
Inode blocks per group:   484
Flex block group size:    16
Reserved blocks uid:      0 (user root)
Reserved blocks gid:      0 (group root)
Journal backup:           inode blocks
$ ls -lad mydir1/
drwxr-xr-x 2 user user 4096 Jan 22 16:04 mydir1/

So the actual bytes used to represent a directory are 4096, even though the contents of the directory are much larger.

List files and directories ordering by size.


Code:
$ du -sm * | sort -n
3   mydir1
101   100m

The above method works for listing both for files and directories and ordering by size.
Jump to:  
cronNews News Site map Site map SitemapIndex SitemapIndex RSS Feed RSS Feed Channel list Channel list


Delete all board cookies | The team | All times are UTC - 5 hours [ DST ]



phpBB SEO