directory size in linux

Bash scripting forum.

directory size in linux

Postby james on January 29th, 2008, 4:44 pm

How do you find the size of a directory in bash (using ls does not return the size). I am interested in determining which directories have large amounts of
data in them so i can look at freeing up space if they are not important.
james
Member
 
Posts: 18
Joined: January 18th, 2008, 2:27 pm

Re: directory size in linux

Postby kamil on January 29th, 2008, 5:13 pm

using du and sort:
Code: Select all
du |sort -nr

using ls and grep (unsorted):
Code: Select all
ls -lahR |grep -B1 total
User avatar
kamil
Linux Adept
 
Posts: 118
Joined: January 18th, 2008, 1:22 am
Location: Brisbane, Australia

Re: directory size in linux

Postby james on January 29th, 2008, 6:25 pm

The du solution works well, however I had an error
Code: Select all
du: fts_read failed: Permission denied

apparently this happens when a directory on ReiserFS is not executable or something (?). I had to change the permissions on the dirs to get it to work.

After fooling around for a while, i came up with
Code: Select all
ls -laR | sed -n '/^$/{N;N;s/\n//g;s/:total//;p}' | awk '{print $2" "$1}' | sort -gr | head -n 20

This monstrosity prints the 20 largest directories from whichever directory you are currently in.
james
Member
 
Posts: 18
Joined: January 18th, 2008, 2:27 pm

Re: directory size in linux

Postby Brett on February 1st, 2008, 9:21 pm

try this
Code: Select all
du -hs <directory_name>

disk usage which is human readable and summary usage :D
Image
User avatar
Brett
Member
 
Posts: 28
Joined: January 22nd, 2008, 7:00 pm

Re: directory size in linux

Postby james on February 5th, 2008, 12:37 pm

haha, nice. I should really read man pages more often :)
james
Member
 
Posts: 18
Joined: January 18th, 2008, 2:27 pm

Re: directory size in linux

Postby Brett on February 5th, 2008, 7:31 pm

just to clear up what the difference is between "du *" and "du -s *"

du * (from /)

does this

Code: Select all
8.0K    etc/hal/fdi/policy
8.0K    etc/hal/fdi/preprobe
8.0K    etc/hal/fdi/information
32K     etc/hal/fdi
40K     etc/hal
32K     etc/pam_pkcs11
200K    etc/alternatives
24K     etc/default
56K     etc/bluetooth
40K     etc/X11/fontpath.d
8.0K    etc/X11/xinit/Xclients.d
24K     etc/X11/xinit/xinitrc.d
72K     etc/X11/xinit
16K     etc/X11/twm
8.0K    etc/X11/applnk
44K     etc/X11/xdm
224K    etc/X11
24K     etc/cron.weekly
8.0K    etc/NetworkManager/dispatcher.d
8.0K    etc/NetworkManager/VPN
24K     etc/NetworkManager
8.0K    etc/subversion
28K     etc/purple
40K     etc/httpd/conf.d
68K     etc/httpd/conf
128K    etc/httpd
12K     etc/acpi/actions
16K     etc/acpi/events
36K     etc/acpi
168K    etc/profile.d
4.0K    etc/libpaper.d


it shows the usage of the directory and all the sub-directories, and if all we want to know is the usage of just every directory hanging off the current (summarised) then du -s * does this

Code: Select all
5.8M    bin
12M     boot
84K     dev
108M    etc
Image
User avatar
Brett
Member
 
Posts: 28
Joined: January 22nd, 2008, 7:00 pm


Return to Bash

Who is online

Users browsing this forum: No registered users and 0 guests

cron

dsplabs homelinux bloglinux forums new! travel photographyawklores new! cryptographyjames' home
©2009 dsplabs.com.au