26

I have 20GB for my Mint-KDE 18 root partition. There is no extra home partition. I am doing nothing special, just Chrome, KRDC, Teamviewer and the partition was half empty. One thing I did was copying in Dolphin from webdavs repo on the internet to my network drive via samba. Nothing was stored on my PC.

Now I got the message that my disk is full. When I open my root or home directory in Dolphin it says 0 MB free.

What is the fastest way via terminal to view my directories and files, biggest first or newest etc?

einpoklum
  • 9,515
Hrvoje T
  • 1,031
  • gdmap is pretty good too. – oals Oct 05 '16 at 11:16
  • 4
    Suggesting non-standard tools is somewhat unhelpful. Where would you install them ?! – MSalters Oct 05 '16 at 11:28
  • 5
    @MSalters: in the few MB you free up after deleting some low-hanging fruit like your browser cache, /var/tmp, or something else that some variation on du | sort | less finds. Once you start tidying up, you might want to continue tidying beyond the point where your system is barely usable, GUI tools can make that easier if the sorted du output is messy. – Peter Cordes Oct 05 '16 at 12:52
  • 1
    Filelight is also a great GUI option. Not a valid answer here, though. It's useful for installing before you run out of disk space :) – stanri Oct 05 '16 at 13:09
  • I would add Konqueror->view->"view mode"->"file size view" to the suggested GUI tools (requires the konq-plugins package on Ubuntu). – KIAaze Oct 05 '16 at 14:40
  • 1
    @MSalters So maybe then Baobab is a better choice, it looks like it comes bundled with Mint by default, so no need to install anything. – Fiksdal Oct 05 '16 at 14:59
  • If you have several file-systems and mount them together - eg. /home and /var are really separate file-systems, and are mounted onto /home and /var in the / (root) file-system - you may want to check the file-systems (eg. / ) without other mounted "on top" of them. Sometimes there may be files and directories under directories usually used as mount-points. These will be invisible when the other file-system is mounted on top of it, but will still use space... you just can't see them. For example, you may have once used the computer without /home or /var mounted. – Baard Kopperud Oct 05 '16 at 15:26
  • 1
    Duplicate of http://unix.stackexchange.com/questions/3961/how-to-understand-whats-taking-up-space and http://unix.stackexchange.com/questions/125429/tracking-down-where-disk-space-has-gone-on-linux and http://unix.stackexchange.com/questions/113840/whats-eating-my-disk-space and http://unix.stackexchange.com/questions/37221/finding-files-that-use-the-most-disk-space – BlueRaja - Danny Pflughoeft Oct 05 '16 at 21:18
  • In case anybody would deal with this on Windows, try SpaceSniffer. It also shows disk activity in the folders by blinking the respective rectangle. Really cool tool – Vitas Oct 05 '16 at 21:56

11 Answers11

40
du -h -d 1 /

This will display the size for all of the top-level directories in your root directory in 'human readable' format. You can also just do

du -h -d 1 / | grep '[0-9]\+G' 

to only see the ones taking a couple GB or more.

For a more granular level of detail, do something like

ls -R -shl / | grep '^[0-9.]\{4,12\}\+[KG]'

which will show all files in and below your root directory that are 1G or over in size.

** note that you might need to prepend sudo to the commands above.

edit -- just saw you want them sorted by newest or largest

Try this

du -h -d 1 / | grep '[0-9]\+G' | sort -h
Wildcard
  • 36,499
John Allard
  • 1,368
  • 3
    With a 20GB root partition including home, du -h / might also be of relevance. Some old boot images might take up to a GB and probably temporary files are overcrowding. – FelixJN Oct 05 '16 at 07:05
  • 4
    You may want to add the -x option which keeps on one file system; there is no need to count the size of /run, /proc/, /sys, or whatever you have on virtual filesystems (or possibly even distinct real filesystem, like the /home partition, even though this OP is not the case). – MariusMatutiae Oct 05 '16 at 07:48
  • Thanks! I used du -h -d 1 /home/user | grep '[0-9]\+G' | sort -h to see only my home. I found recoll to be 2.5GB, uninstalled and removed that. However, .cache directory is 5GB. Is it because I'm copying things from one place to another? Will it disappear after reboot? – Hrvoje T Oct 05 '16 at 07:57
  • 2
    You should run du -h -d 1 /home/user/.cache | grep '[0-9]\+G' | sort -h to see what's taking up so much space, then figure out if it's okay to clear it. But generally, yes, it's fine to delete your cache. You only put stuff in there if you know it's okay to lose. – John Allard Oct 05 '16 at 08:11
  • Also you might find useful this tool fdupes. – Kyslik Oct 05 '16 at 09:25
  • Extra points for mentioning sort -h. I've been using it every day since I discovered it. – SusanW Oct 05 '16 at 12:04
  • 1
    On RedHat 6 (maybe other older distros?) the -d option doesn't work, however du --max-depth=1 will work. – Digital Chris Oct 05 '16 at 14:37
  • 1
    Personally I use du -k / | sort -nr - thus showing directories in order of size. I may also pipe it further to head, to show just the top 10 - du -k / | sort -nr | head. If I know where the problem lay (cough /home cough), I may just check that directory du -k /home | sort -nr. I also use for to for example check all home-directories - cd /home : for i in * ; do du -k | sort -nr > /tmp/home-${i}.usage ; done. – Baard Kopperud Oct 05 '16 at 15:13
  • @JohnAllard What is the difference beetwen grep '[0-9]G' and grep '[0-9]\+G' – Hrvoje T Oct 06 '16 at 06:06
  • 1
    @HrvojeT the '+' operator in a regular expression means "match one or more of these items", so it accounts for more than digit that can be present. The backslash is needed so it's not interpreted as an actual '+' character, rather the regex operator. – John Allard Oct 06 '16 at 07:15
26

Instead of all the manual commands in the other answers, I recommend a tool specifically built for this purpose: ncdu, install with the package manager of your choice.

It only is a small command line utility, so you should be able to install, when you just make a little bit space.

Then, just run

$ ncdu /

to analyze the entire system.

See https://dev.yorhel.nl/ncdu/scr for screenshots: enter image description here

knub
  • 369
12

My favorite command for this purpose is

du | sort -nr | head -20

du without options lists all subdirectories and their sizes.
sort sorts by size. n asks for a numerical sort. r reverses the result to largest first.
head -20 limits the output to 20 lines.

By listing directories instead of files, you will find directories that contains a large number of small files.

For sort to work properly we can't use human-readable output, but the largest is the largest even if you can't quite parse its size.

The output for a directory will include its subdirectories, so you will see .cache listed before .cache/chromium (or whatever)

  • Hey thanks! After this command I saw that a .recoll directory which I deleted via Dolphin is still in .local/share/Trash/files/.recoll. Why is this file still there? Is it save to remove everything from `.local/share/Trash/'? – Hrvoje T Oct 05 '16 at 12:14
  • Nevermind, I removed it with rm -r – Hrvoje T Oct 05 '16 at 12:27
  • 3
    You can sort human-readable sizes using sort -h. – benvd Oct 05 '16 at 12:30
  • I used to use du -m | sort -n, to get all sizes in the same units (MB), but sort -h is probably a lot better now that numbers in megabytes are long enough to make your eyes glaze over. There are graphical equivalents, e.g. k4dirstat is quite good. – Peter Cordes Oct 05 '16 at 12:49
6

What is the fastest way via terminal to view my directories and files, biggest first or newest etc?

You can use the command find to display files with their sizes in human readable format :

find / -type f -size +300000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'

adjust the +300000k value.

GAD3R
  • 66,769
  • 1
    Could you explain this command to me? What does {} and $9 and $5 do? They are placeholders? – Hrvoje T Nov 13 '17 at 12:56
  • 1
    @HrvojeT to see the difference , run find / -type f -size +300000k -exec ls -lh {} \; you will get 9 columns , we need just to see the path to file and the size of the file , we use $9 (the clumun that indicate the path) and $5 (column that indicate the size) . – GAD3R Nov 13 '17 at 13:11
  • 1
    @HrvojeT To get more info The GNU Awk User’s Guide – GAD3R Nov 13 '17 at 13:23
4

Use du with the -h --max-depth= parameters (settings of 1 to 3 are useful) to drill down on where space is used. If these numbers do not add up, try stopping non-essential services like web servers, log daemons, nfs (or just reboot) - running processes can take space on the filesystem too by keeping an already unlinked file open (often happens with logging services) and check if the space usage decreases.

GAD3R
  • 66,769
rackandboneman
  • 489
  • 2
  • 5
4

Before running the more time-consuming du -h command suggested by others, have a look at some folders you can clear easily and often contain junk. You might want to du -h in each of them if they contain a lot of files, before the global du -h.

  1. /tmp
  2. /var/tmp; but more careful here before, see this page here on the site about how it differs from /tmp.
  3. /home/your_user_name - your home directory: you may very well have downloaded some large files here and forgotten about them, or built something which pulls/generates a huge amount of data and doesn't delete it etc. Of course, the files here are mostly ones you placed, so you should know what's safe to delete and what isn't.
  4. /opt and /usr/local - not relevant in your specific case, since you indicated you haven't installed anything special, but for other people reading this question - these are typical destinations for installing software that's not part of your Linux distribution. Make sure you actually need that software and are not just keeping it lying around.
einpoklum
  • 9,515
  • 1
    If things are installed via the /usr/local path, also running make clean in source directories can reclaim considerable space. – rackandboneman Oct 05 '16 at 10:55
3

There is a nice piece of cross-platform freeware called JDiskReport which includes a GUI to explore what's taking up all that space.

Example screenshot:
JDiskReport screenshot

Of course, you'll need to clear up a little bit of space manually before you can download and install it, or download this to a different drive (like a USB thumbdrive).

WBT
  • 139
3

I think baobab comes standard on Linux Mint. It should let you zero in visually on which directories use the most space.

enter image description here

There is a pretty good chance that a lot of it is browser cache. The visual tool will let you interactively see exactly where most of it is.

2

You can use standard tools like find and sort to analyze your disk space usage.

List directories sorted by their size:

find / -mount -type d -exec du -s "{}" \; | sort -n

List files sorted by their size:

find / -mount -printf "%k\t%p\n" | sort -n
scai
  • 10,793
2

I'm late to the party. It seems like everybody has their own favorite version of du | sort with different flags.

I've always found it easy to remember "ducks":

du -cks * | sort -rn | head -11

From the du manpage:

   -c, --total
          produce a grand total

   -k     like --block-size=1K

   -s, --summarize
          display only a total for each argument

I got the tip from the book "Linux Server Hacks: 100 Industrial-Strength Tips and Tools" by Rob Flickenger (O'Reilly, 2003).

In Hack #15 (Playing Hunt the Disk Hog), the author recommends adding this one-liner to your .profile:

alias ducks='du -cks |sort -rn |head -11'

In practice, though, I generally don't get around to aliasing the command because I've used it so many times that I have memorized it.

  • I agree. It's better for a beginner not to use aliases. Could you explain what cks does? Is it in MB or? – Hrvoje T Oct 06 '16 at 05:51
  • I added the info (from man du) to my answer above -- but honestly, what it really does for me is serve as a mnemonic device to help me remember the command! ;) – Matt Obert Oct 06 '16 at 10:45
-3

I think the below command will show u the detailed info motion

cd /

du -hs * | sort -n
GAD3R
  • 66,769
Rajesh
  • 1