LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-17-2013, 06:38 PM   #1
hugheskc@yahoo.com
LQ Newbie
 
Registered: May 2013
Posts: 2

Rep: Reputation: Disabled
Post list sub-directories and disk space consumed


I need a script that will start with the PWD and list sub-directories and the total amount of disk space consumed by all files in all directories under each sub directory.

bytes directory
----- ------------------
800m app
2200m product
350000m admin
456000m diag

Thank you for your help (:^)
 
Old 05-17-2013, 07:15 PM   #2
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 306

Rep: Reputation: 121Reputation: 121
This is not in the format you are asking for, but it's a start:
Code:
#!/bin/bash
find . -maxdepth 1 -mindepth 1 -type d | while read -r file
do
du -sh "$file"
done
In a certain directory at my end, this is the output:
Code:
1.9G    ./Documents
2.0G    ./Pictures
36G     ./Music
1.3G    ./from segundo
68M     ./downloads
The find and du commands have various other options that might be useful to you.
 
Old 05-17-2013, 11:40 PM   #3
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
This could be done simply using commands.

To check size of all directories residing in pwd:
Code:
~$ du -sk ./*
To check size of all directories, including files, residing in pwd:
Code:
~$ find . -exec du -sk {} \;
 
Old 05-18-2013, 12:07 AM   #4
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 306

Rep: Reputation: 121Reputation: 121
Quote:
Originally Posted by shivaa View Post
To check size of all directories residing in pwd:
Code:
~$ du -sk ./*
To check size of all directories, including files, residing in pwd:
Code:
~$ find . -exec du -sk {} \;
These do not work the same. The first command shows the size of files as well as directories. The second command shows all that, plus the size of each subdirectory, and each file present in subdirectories.

However I thank you for reminding me of -exec. I guess I am still a newbie.
Code:
find . -maxdepth 1 -mindepth 1 -type d -exec du -sh {} \;
and upon further consideration, I see that this shows only directories:
Code:
du -sh */

Last edited by Beryllos; 05-18-2013 at 12:28 AM. Reason: explained how the command outputs differ
 
Old 05-18-2013, 12:59 AM   #5
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by Beryllos View Post
These do not work the same. The first command shows the size of files as well as directories. The second command shows all that, plus the size of each subdirectory, and each file present in subdirectories.
Please re-read my post. First command will show sizes of all subdirectories only whereas second one will give sizes of all subdirectories as well as files.
 
Old 05-18-2013, 03:04 AM   #6
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Quote:
Originally Posted by shivaa View Post
Please re-read my post. First command will show sizes of all subdirectories only whereas second one will give sizes of all subdirectories as well as files.
Sorry, but that is not true. If there are files present in the current directory these will be shown. It also only shows one level deep.

Code:
$ ls -l
drwxr-x--- 6 druuna druuna  4096 jan 13 12:30 Fife
drwxr-x--- 8 druuna druuna  4096 mei  7 15:43 LFS
drwxr-x--- 2 druuna druuna  4096 jan 13 16:11 Log
drwxr-x--- 2 druuna druuna 69632 mei 11 10:03 Tmp
-rw-r----- 1 druuna druuna   102 mei 18 08:23 awk_script
-rw-r----- 1 druuna druuna    45 mei 18 08:20 file1
-rw-r----- 1 druuna druuna    45 mei 18 08:20 file2
-rw-r----- 1 druuna druuna    42 mei 18 08:20 file3

$ du -sk ./*
24      ./Fife
313992  ./LFS
8       ./Log
68      ./Tmp
4       ./awk_script
4       ./file1
4       ./file2
4       ./file3
To show all the subdirectories and not just one level the s switch needs to be removed, that combined with Beryllos' answer you get this:
Code:
$ du -k */
4       Fife/exile/Blaat
8       Fife/exile
4       Fife/192.168.100.11
4       Fife/10.0.100.11
4       Fife/stasis
24      Fife/
4       LFS/chapter7
4       LFS/chapter8
4       LFS/config
4       LFS/chapter6
4       LFS/chapter5
4       LFS/chapter4
313992  LFS/
8       Log/
68      Tmp/
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] running out of disk space and needing to transplant /home directories unclejed613 Slackware 11 11-06-2009 12:05 AM
Are Quota Limit and Disk Size consumed different? Nakano Linux - Newbie 1 07-19-2009 04:08 AM
Available hard disk space reducing(consumed) extremely fast kagara Linux - Software 1 09-16-2008 08:37 AM
To list files consuming disk space ZAMO Linux - General 4 05-05-2008 07:33 AM
how can i find space consumed by this user izrafel Linux - General 3 06-15-2007 05:21 AM


All times are GMT -5. The time now is 06:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration