Largest files in directory linux

How To Find Largest Top 10 Files and Directories On Linux / UNIX / BSD

How to find out top 10 files and directories on Linux or Unix

There is no simple command available to find out the largest files/directories on a Linux/UNIX/BSD filesystem. However, combination of following three commands (using pipes) you can easily find out list of largest files:

Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Linux and Unix-like OS
Est. reading time 3 minutes

Steps to find Largest Directories in Linux

  1. du command : Estimate file space usage.
  2. sort command : Sort lines of text files or given input data.
  3. head command : Output the first part of files i.e. to display first 10 largest file.
  4. find command : Search file.

How to find out top Directories and files in Linux

Type the following command at the shell prompt to find out top 10 largest file/directories:
# du -a /var | sort -n -r | head -n 10
Sample outputs:

If you want more human readable output try (works with GNU/Linux du version/user only):
$ cd /path/to/some/where
$ du -hsx * | sort -rh | head -10
$ du -hsx — * | sort -rh | head -10
Where,

  • du command -h option : display sizes in human readable format (e.g., 1K, 234M, 2G).
  • du command -s option : show only a total for each argument (summary).
  • du command -x option : skip directories on different file systems.
  • sort command -r option : reverse the result of comparisons.
  • sort command -h option : compare human readable numbers. This is GNU sort specific option only.
  • head command -10 OR -n 10 option : show the first 10 lines.

The above command will only work of GNU/sort is installed. Other Unix like operating system should use the following version (see comments below):

Find the largest file in a directory and its subdirectories using the find command

Type the following GNU/find command:

You can skip directories and only display files, type:

Hunt down disk space hogs with ducks

Let us find out top directories and files using disk space in Linux or Unix with help of the following bash shell alias:

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Run it as follows to get top 10 files/dirs eating your disk space:
$ ducks
Sample outputs:

Fig.01 Finding the largest files/directories on a Linux or Unix-like system

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

Great, but what if I only want the largest files and not the directories?

To find out largest file only use command ls as follows in current directory:
ls -lSh . | head -5
Output:
-rw-r–r– 1 vivek vivek 267M 2004-08-04 15:37 WindowsXP-KB835935-SP2-ENU.exe
-rw-r–r– 1 vivek vivek 96M 2005-12-30 14:03 VMware-workstation-5.5.1-19175.tar.gz
ls -lSh /bin | head -5
You can also use find command but not du:
find /var -type f -ls | sort -k 7 -r -n | head -10

Hope this helps

And yes to find the smallest files use command:
ls -lSr /var

Or use find command with -size flag.
find / -type f -size +20000k -exec ls -lh <> ; | awk ‘< print $8 “: ” $5 >’

Read man page of find for more info.

“find / -type f -size +20000k -exec ls -lh <> ; | awk ‘< print $8 “: ” $5 >’”

needs to have the exec altered

find / -type f -size +20000k -exec ls -lh <> \; | awk ‘< print $8 “: ” $5 >’

Also, I find this output easier to read

find . -type f -size +20000k -exec ls -lh <> \; | awk ‘

How do I can list all the files in several directories and at the end write the totat of all the files and directories.I’m using the du command as fallow:
du -sh /public/base/sites/F*/*20070115*

this command give me the size of all the files but not the global total.

can somebody help me. please write me. john_fernandez@verizon.com.do

“If you want more human readable output try:

# du -ha /var | sort -n -r | head -n 10”

Im pretty sure that this will put 999kb above 1gb so I don’t think that this works.

This does not work.

# du -ha /var | sort -n -r | head -n 10″

as Joe says this ignores files over 1gb

You could try this, gives a human readable output in MB

find . -type f | xargs ls -s | sort -rn | awk ‘‘ | head

Human readable version:

for X in $(du -s * | sort -nr | cut -f 2); do du -hs $X ; done

If you set du to human readable I think it will not sort the way you really want.

For the above problems. I would like to find a way to list only the last level directories’ sizes.

(I want to filter somehow this:
/home
/home/user
/home/user/mail

I just want to see the lasts of the tree!)

this is what i use.

for i in G M K; do du -ah | grep 9$i | sort -nr -k 1; done | head -n 11

Worked like charm

find . -type f -print0| xargs -0 ls -s | sort -rn | awk ‘’ | head

! -print0 for filenames with spaces …
(and xargs -0 combined)

No wonder windows rule

Yeah you just go ahead and wait the 3 hours this search would take on Windows.

How about the 3 hours it takes to read through a bunch of unexplained programming nonsense. I swear half of the time all Linux guys do is insult other users….Example, the first listing is great as it begins to explain what the flags do, but I have no idea were to put some of them, pipes are not explained…ect

This is why Winblows users should not try and use Linux, they are very unintelligent and lack the ability to look up simple things.

Fast forwarda few years, Linux won, bro 🙂

“Late last week, hell had apparently frozen over with the news that Microsoft had developed a Linux distribution of its own. The work was done as part of the company’s Azure cloud platform, which uses Linux-based network switches as part of its software-defined networking infrastructure.” — SOURCE HERE.

Winfan, I read this page wondering how to do these things in Windows — can you post instructions? Thanks!

This may vary depending on the version of Windows you’re using, but the basic procedure is: open the find/search window, go to the advanced options, and there will be an option there to enter in a size parameter. Simple.

In XP, press F3 or go Start->Search. Choose “All files and folders”, then “More advanced options”, then “What size is it?”, then specify a size.

I believe beez was being sarcastic towards Winfan as was his right after Winfans brainless comment. Most Linux distro GUI’s come with search function just like Windows GUI.

Now just try to do the search on command line on Windows (server)…

@winfan… how do you do this in windows? you don’t 😛

c:\>dir /S /O-S | more

The simple dir /S command from c:\ will give you all files and directories from c:\ all the way through the drive and will sort from largest to smallest running through each directory. You can filter using /A if you’d like to restrict by hidden, system, archive files, read only files etc. and passing the output to another windows command if you need to further restrict or search in the files for something like “show me all the files on my hard drive over 6MB that contain the word ‘log’ from largest to smallest.”

/O will Specify the sort order for the files with the S after it sorting by file size (smallest first) putting the – in front of the S reverses the order.

| more – you’re a unix dude, you should know what this means…

But if someone is doing some cleanup through their harddrive, this is the simple command I’d start with.

Just a note about the cockyness or us Unix admins (as I happen to be one now)
Not everyone that uses windows started using it with a mouse kid. Also not everyone who prefers windows is not cross-platform… We were running 64 bit clustered NT boxes on RISC processors at Digital Equipment Corporation with failover and failback in 1996 brother. Don’t believe me? Find a really old copy of Windows NT ver 3.51 open it and you’ll see two folders NT and Alpha.

The Department of Veterans Affairs had no problems with ever needing to reboot a “lousy unreliable windows box” because the Intel platform itself was the problem, not windows. We ran Alpha on 64 bit RISC processors and it was just as reliable as any Unix box or Mainframe we had. I had a Jensen Alpha running an exchange server for 5 years, and we only rebooted it every 6 months for giggles…

Windows machines are made to be used by the masses which means more dumbasses can kinda run one. A good Admin is a good Admin, no matter what platform. Be nice and be helpful or don’t post.

Notafan wrote:
@winfan… how do you do this in windows? you don’t 😛

Well, actually, there *is* cygwin (unix commands for Windows systems)
http://www.cygwin.com/

I find the following works rather well…

It lists all files or directories bigger than 50MB (just change size>50 to alter that) in the current directory (change the “.” to a directory path to specify another one) in a friendly, human-readable way and happily plays with spaces (I used it on an NTFS mount in fact).

I get a syntax error when coptying and pasting this command

Источник

Linux find largest file in directory recursively using find/du

I have 500GB SSD installed on my Linux server. My web server is running out of the disk space. I need to find a biggest or largest file concerning file size on the disk. How do I find largest file in a directory recursively using the find command?

To find a big file concerning file size on disk is easy task if you know how to use the find, du and other command. The du command used to estimate file space usage on Linux system. The output of du passed on to the sort and head command using shell pipes. Let us see how to find largest file in Linux server using various commands.

Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux
Est. reading time 3 minutes

Linux find largest file in directory recursively using find

The procedure to find largest files including directories in Linux is as follows:

  1. Open the terminal application
  2. Login as root user using the sudo -i command
  3. Type du -a /dir/ | sort -n -r | head -n 20
  4. du will estimate file space usage
  5. sort will sort out the output of du command
  6. head will only show top 20 largest file in /dir/

Linux find a biggest files in /

Linux find large files quickly with bash alias

One can hunt down disk space hogs with ducks bash shell alias

How To Find Largest Top 10 Files and Directories On Linux / UNIX / BSD

Finding largest file recursively on Linux bash shell using find

One can only list files and skip the directories with the find command instead of using the du command, sort command and head command combination:
$ sudo find / -type f -printf «%s\t%p\n» | sort -n | tail -1
$ find $HOME -type f -printf ‘%s %p\n’ | sort -nr | head -10
Here is what I got on my systems:

Where, find command options are as follows:

  • $HOME – Directory search for files.
  • -type f – Search for regular files only.
  • -printf ‘%s %p\n’ – Force find to use print format on the scren, interpreting \ escapes and % directives. The %s will print file’s size in bytes. Show file name using %p . This speailized output makes it easy to sort out file names using the sort command.

The -n is for numeric sort and the -r passed to sort will reverse the result of comparisons. The head command is used to control and show the first part of files. In other words, only display the top 10 results from previous commands.

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Great! I found the largest files on my disk. What next?

Depend upon file/dir type you can either move or delete the file. For example, you cannot remove or move the Linux kernel or diver directories. To delete unwanted file on Linux use the rm command:
rm -i -v /path/to/file
To get rid of all files and its sub-directories recursively use following command:
rm -rf /path/to/folderName
To move file to a usb pen mounted at /mnt/usb/, run the mv command:
mv /path/to/large/file/ /mnt/usb/

Conclusion

You just learned how to search, find and list largest or biggest directories/files in Linux using the combination of du/find and other commands. For more info see this page or man pages of du and find commands:
man du
man find
man sort
man head
man tail

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Источник

Читайте также:  Настроить windows для домашнего медиа сервера
Оцените статью