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

FAQ
It is currently Sat Aug 19, 2017 2:44 am


System administration, processes administration, jobs, cron, resources, limits, shells, ssh, telnet.

Author Message
mandrei99
Post  Post subject: How to list the largest files in a directory in Linux/FreeBSD  |  Posted: Thu Jan 22, 2015 5:14 pm

Joined: Tue Aug 04, 2009 9:16 am
Posts: 245

Offline
 

How to list the largest files in a directory in Linux/FreeBSD

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.





Top
Display posts from previous:  Sort by  
E-mail friendPrint view

Topics related to - "How to list the largest files in a directory in Linux/FreeBSD"
 Topics   Author   Replies   Views   Last post 
There are no new unread posts for this topic. How to list PCI devices in Linux - lspci

mandrei99

0

1268

Mon Jul 22, 2013 10:03 am

mandrei99 View the latest post

There are no new unread posts for this topic. Linux: List kernel supported filesystems

debuser

0

434

Tue Mar 10, 2015 5:45 pm

debuser View the latest post

There are no new unread posts for this topic. Linux: list block devices UUID

debuser

0

783

Tue Mar 10, 2015 5:46 pm

debuser View the latest post

There are no new unread posts for this topic. How to list PCI devices in FreeBSD - pciconf

mandrei99

0

7247

Mon Jul 22, 2013 9:58 am

mandrei99 View the latest post

There are no new unread posts for this topic. FreeBSD - list disks drive in the system with atacontrol and camcontrol

mandrei99

0

6962

Tue Jan 20, 2015 3:08 am

mandrei99 View the latest post

There are no new unread posts for this topic. GPG key retrieval failed: [Errno 5] OSError: [Errno 2] No such file or directory: '/etc/pki/rpm-gpg/

mandrei99

0

1257

Tue Jan 20, 2015 4:15 am

mandrei99 View the latest post

There are no new unread posts for this topic. Linux clear screen / terminal

mandrei99

2

4575

Wed Mar 04, 2015 8:52 am

Guest View the latest post

There are no new unread posts for this topic. How to prevent Linux SSH client from disconnecting using ServerAliveInterval

mandrei99

0

14503

Fri Jan 09, 2015 8:26 pm

mandrei99 View the latest post

There are no new unread posts for this topic. Change default editor to VI in Debian Linux

mandrei99

0

631

Fri Jan 16, 2015 6:59 pm

mandrei99 View the latest post

There are no new unread posts for this topic. How to print filesystem super block information in Linux

mandrei99

0

6905

Mon Jul 08, 2013 6:56 am

mandrei99 View the latest post

 

Who is online
Users browsing this forum: No registered users and 0 guests
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum
Jump to:  
News 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