Linux find directories by size

What’s a command line way to find large files/directories to remove and free up space?

What’s odd is I have two servers that are running the same thing. One is at 50% disk usage and the other is 99%. I can’t find what’s causing this.

13 Answers 13

If you just need to find large files, you can use find with the -size option. The next command will list all files larger than 10MiB (not to be confused with 10MB):

If you want to find files between a certain size, you can combine it with a «size lower than» search. The next command find files between 10MiB and 12MiB:

find / -size +10M -size -12M -ls 

apt-cache search ‘disk usage’ lists some programs available for disk usage analysis. One application that looks very promising is gt5 .

From the package description:

Years have passed and disks have become larger and larger, but even on this incredibly huge harddisk era, the space seems to disappear over time. This small and effective programs provides more convenient listing than the default du(1). It displays what has happened since last run and displays dir size and the total percentage. It is possible to navigate and ascend to directories by using cursor-keys with text based browser (links, elinks, lynx etc.)

Screenshot of gt5

On the «related packages» section of gt5, I found ncdu . From its package description:

Ncdu is a ncurses-based du viewer. It provides a fast and easy-to-use interface through famous du utility. It allows to browse through the directories and show percentages of disk usage with ncurses library.

Источник

find directories having size greater than x MB

Is it possible to find the directories only having size large than x MB. Suppose, I want to find all the directories only whose size is large than 1000MB with only 1 maxdepth under /home, how to find it in ?

Stack Overflow is a site for programming and development questions. This question appears to be off-topic because it is not about programming or development. See What topics can I ask about here in the Help Center. Perhaps Super User or Unix & Linux Stack Exchange would be a better place to ask. Also see Where do I post questions about Dev Ops?

Читайте также:  Export proxy for linux

2 Answers 2

If I’m interpreting your question right, I think this might be what you want:

cd /home du -sm * | awk '$1 > 1000' 

This will show all directories in /home that contain more than 1000MB. If your version of du doesn’t support -m , you can use du -sk and adjust the awk bit to look for more than 1,000,000KB instead.

According to the manpage, the -k option is POSIX compliant but the -m option is not.

So the following is more portable (i.e. if you’re on BSD, it will still work) but essentially does the same:

du -sk * | awk -v m=1000 '$1 > 1024*m' 

Just set the awk variable m to the number of megabytes you would like as your cut-off.

I found this very useful for moving a batch of files, hence posting here for others’ benefit.

To extend this to move all files meeting your criteria to another directory, you can adjust the awk command to print just the bit you need for the move (excluding the size) and then loop:

# moving all directories meeting the size criteria to another location: for d in $(du -sk * | awk -v m=1000 '$1 > 1024*m ') do mv $d $DESTINATION done 

Источник

How to Find Out Top Directories and Files (Disk Space) in Linux

As a Linux administrator, you must periodically check which files and folders are consuming more disk space. It is very necessary to find unnecessary junk and free up from your hard disk.

This brief tutorial describes how to find the largest files and folders in the Linux file system using du (disk usage) and find command. If you want to learn more about these two commands, then head over to the following articles.

How to Find Biggest Files and Directories in Linux

Run the following command to find out top biggest directories under /home partition.

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

The above command displays the biggest 5 directories of my /home partition.

Find Largest Directories in Linux

If you want to display the biggest directories in the current working directory, run:

Let us break down the command and see what says each parameter.

  1. du command: Estimate file space usage.
  2. a : Displays all files and folders.
  3. sort command : Sort lines of text files.
  4. -n : Compare according to string numerical value.
  5. -r : Reverse the result of comparisons.
  6. head : Output the first part of files.
  7. -n : Print the first ‘n’ lines. (In our case, We displayed the first 5 lines).

Some of you would like to display the above result in human-readable format. i.e you might want to display the largest files in KB, MB, or GB.

Читайте также:  Файл dwg чем открыть линукс

The above command will show the top directories, which are eating up more disk space. If you feel that some directories are not important, you can simply delete a few sub-directories or delete the entire folder to free up some space.

To display the largest folders/files including the sub-directories, run:

Find out the meaning of each option using in above command:

  1. du command: Estimate file space usage.
  2. -h : Print sizes in human-readable format (e.g., 10MB).
  3. -S : Do not include the size of subdirectories.
  4. -s : Display only a total for each argument.
  5. sort command : sort lines of text files.
  6. -r : Reverse the result of comparisons.
  7. -h : Compare human readable numbers (e.g., 2K, 1G).
  8. head : Output the first part of files.

Find Out Top File Sizes Only

If you want to display the biggest file sizes only, then run the following command:

# find -type f -exec du -Sh <> + | sort -rh | head -n 5

To find the largest files in a particular location, just include the path beside the find command:

# find /home/tecmint/Downloads/ -type f -exec du -Sh <> + | sort -rh | head -n 5 OR # find /home/tecmint/Downloads/ -type f -printf "%s %p\n" | sort -rn | head -n 5

The above command will display the largest file from /home/tecmint/Downloads directory.

That’s all for now. Finding the biggest files and folders is no big deal. Even a novice administrator can easily find them. If you find this tutorial useful, please share it on your social networks and support TecMint.

Источник

How to find the largest directories or largest files? [duplicate]

Under Linux, I’m looking for a command to list the largest file and/or the largest directories under a directory.

The most useful tool I’ve found is xdiskusage (xdiskusage.sourceforge.net) This shows graphically where the files are — by size. Great tool! (and it works directly with X11)

10 Answers 10

du with no arguments summarizes disk usage by directories. du with -a produces the same directory information and includes the disk usage for individual files as well.

A utility called ncdu will give you the information you are looking for.

On OS X, it can be installed using Homebrew:

Fyi, this is an ncurses (text-based UI) application. So, no need to extensively discuss CLI parameters and whatnot; just install it and execute ncdu in the directory you’d like to analyze.

Following command shows you one level of directories and their sizes

du --max-depth=1 /path | sort -r -k1,1n 

If one of them really sticks out (the last one on the list is the largest due to sort -r ), then you re-run the command on that directory, and keep going until you find the offending directory / file.

Читайте также:  Linux what is file encoding

If all you want is the ten biggest files just do

find /home -type f -exec du -s <> \; | sort -r -k1,1n | head 

biggest number ends up at the bottom for me no matter if I add sort -r or not. Is there a way to get the biggest number at the top?

You must indicate to sort which column you want to sort by, and that it’s numeric (not alphanumeric). That’s what -k1,1rn would do. By default sort does uses alphanumeric sort on first column.

Yes, it’s sorting correctly with that, but it’s in ascending order low to high numbers no matter if I include sort or sort -r . Am I misunderstanding how the -r works? I guess it’s not a big deal. Your example is very helpful and got me the info I needed.

With the sort I have ( sort (GNU coreutils) 8.13 in Ubuntu 12.04.3) the option -r does not work if -n immediately follows -k ( -k1,1n ). This order of options works: sort -rnk1,1 .

Great answer. On OS X the option is -d 1 instead of —max-depth 1 . What is also useful is -m which then reports in megabyte instead of block count.

Following command will return top 10 biggest files from given /path

du -a -h /path | sort -h -r | head -n 10

I like to use -h options for readability. Both du and sort need to have -h .

This will show the biggest directory/file in a directory in KB. Changing the head value will result in the top x files/directories.

cd /path/to/some/where du -a /var | sort -n -r | head -n 10 du -hsx * | sort -rh | head -10 
ls -A | xargs -I artifact du -ms artifact | sort -nr 

Optionally, you can add a pipe and use head -5

Use du. Try this to order the result:

Try the following one-liner (displays top-20 biggest files in the current directory):

ls -1Rs | sed -e "s/^ *//" | grep "^1" | sort -nr | head -n20 

or with human readable sizes:

ls -1Rhs | sed -e "s/^ *//" | grep "^9" | sort -hr | head -n20 

The second command to work on OSX/BSD properly (as sort doesn’t have -h ), you need to install sort from coreutils .

So these aliases are useful to have in your rc files (every time when you need it):

alias big='du -ah . | sort -rh | head -20' alias big-files='ls -1Rhs | sed -e "s/^ *//" | grep "^5" | sort -hr | head -n20' 

Источник

Оцените статью
Adblock
detector