Command to calculate space for all subdirs under a dir

 
Thread Tools Search this Thread
Homework and Emergencies Emergency UNIX and Linux Support Command to calculate space for all subdirs under a dir
# 8  
Old 12-20-2011
Off-the-wall

Hi.

If you are looking for the space infrequently, then du seems like an appropriate solution, or perhaps allowing some tasks to run in parallel, say for the first-level sub-directories.

However, if you need to see the space more often, then there is a way that can you see it very quickly: make it a separate file system, and use df:
Code:
$ df -h
Filesystem             size   used  avail capacity  Mounted on
rpool/ROOT/s10x_u6wos_07b
                       7.8G   3.8G   2.9G    57%    /
swap                   476M   372K   476M     1%    /etc/svc/volatile

This was produced in:
Code:
OS, ker|rel, machine: SunOS, 5.10, i86pc
Distribution        : Solaris 10 10/08 s10x_u6wos_07b X86

and the results appeared almost instantaneously after the Return was pressed -- try it for yourself.

I have not done this in Solaris (it looks different on my Linux box), but I'm supposing that you'd allocate space on a disk, create a filesystem, create a directory in /, mount the filesystem, and copy the large directory to it, which would take time and disk space. After that, you could use the new directory as usual, and get your space calculations very fast.

It's not a trivial matter to do this, and it is drastic, but if you really, really needed it, then it may be a solution to consider. ... cheers, drl
# 9  
Old 12-20-2011
I use the following line to find where disk usage is going.
Code:
du -a |sort -r -n >/tmp/list

The list contains all directories and files sorted in descending order by size.
# 10  
Old 12-21-2011
Hammer & Screwdriver

Quote:
Originally Posted by otheus
Jayjan jay!! Don't use find with exec unless you have to do stuff inside the exec!

Replace with :
Code:
find $DIR -printf "%k\n" | awk '{sum+=$1}END{print sum}'

@otheus, My system doesnt support -printf option in find command .. Thats why used -exec .. Smilie
Code:
$ uname -sr
SunOS 5.9
$ find $DIR -printf "%k\n"
find: bad option -printf
find: path-list predicate-list
$

# 11  
Old 12-21-2011
Code:
 
find . -type d | xargs du -h

# 12  
Old 12-22-2011
@jayjan_jay

That's incredible to me, since I learned find in 1992 while using a SunOS 4.1 server.

.... (update) ....

Then again, we probably had GNU fileutils installed. Do yourself a favor and install the SFW binaries for find and other GNU fileutils.

ftp://ftp.sunfreeware.com/pub/freewa...sparc-local.gz

There are some dependencies that you must also install. Dependencies: coreutils, libiconv, libintl, and to have /usr/local/lib/libgcc_s.so.1 install either the libgcc-3.4.6 or gcc-3.4.6 or later. You can find them at http://sunfreeware.com/

Last edited by otheus; 12-30-2011 at 06:33 AM..
# 13  
Old 02-10-2012
This is an alias that I have setup for doing a similar thing. It is about as fast as everything else previously listed, and has a clean output [since you seemed to want -h].

Code:
alias lsS='du -s * 2> /dev/null |sort -n |cut -f 2- |while read a; do du -sh "$a" 2> /dev/null; done'

Hope that helps...
# 14  
Old 02-11-2012
Intresting... have tested the behaviour in different scenarios and I can say # du is best and more accurate as compare to any ...

Here's for ur ref ..
####################### First Flushed catched ..
Code:
 [root@SKS Shirish]# sh -c sync; echo 3 > /proc/sys/vm/drop_caches; time  du -sb /var
195404352       /var
real    0m2.597s
user    0m0.244s
sys     0m0.468s
[root@SKS Shirish]# sh -c sync; echo 3 > /proc/sys/vm/drop_caches; time  perl  size.pl
Total Size in Byte:  194019894 `in 4686 files
real    0m3.399s
user    0m0.420s
sys     0m0.528s
[root@SKS Shirish]#

size.pl
Code:
 
[root@SKS Shirish]# cat size.pl
#!/usr/bin.perl -w
use strict;
use File::Find;
my $byte_total;
my $file_total;
find (\&sum_bytes, "/var");
sub sum_bytes
{
 return() unless (-f $File::Find::name);
 $byte_total += -s _; 
 $file_total++;
}
print "Total Size in Byte:  $byte_total `in $file_total files\n";

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Solaris

Running a script in a sub dir with space in it

Hi I am trying to run a script in a sub-directory, that has one space on it: such as: /internaldisk1/Task Logs1, but my entry in cron is not working: 0,5,10,15,20,25,30,35,40,45,50,55 * * * * /internaldisk1/Task\ logs1/./remov_reject.sh > /var/tmp/interd_`date '+%Y%M%d'`.log 2>&1 Please can... (4 Replies)
Discussion started by: fretagi
4 Replies

2. UNIX for Dummies Questions & Answers

Cannot cd to dir with space

I have a script that I am trying to use. I need it to run from a certain directory. I have tried the following: SCRIPT_DIR=$PWD MYDIR="/filemount/files/Move Files" cd "$MYDIR" $SCRIPT_DIR/movefiles.bash I have tried to run it. but I always get "no such file or directory" from the script... (5 Replies)
Discussion started by: newbie2010
5 Replies

3. UNIX for Dummies Questions & Answers

List files older that 7 days in a dir, excluding all subdirs

Hi, I would like to list all files, older than 7 days, in a directory, but exclude all subdirectories in the find command. If I use find . -type f -mtime +7 all files in the subdirs are also included. How can I exclude them? Regards, JW (6 Replies)
Discussion started by: jwbijl
6 Replies

4. Shell Programming and Scripting

Copy files and subdirs from dir to a new dir

Hello Comunity I am trying to make a bash shell script that it copies files and subdirs(with files) to a new dir. I would like the dest_dir to contain only subdirectories with files not other subdirs inside. it called : cpflatdir src_dir dest_dir Pleaze help me! Thank you in... (2 Replies)
Discussion started by: BTKBaaMMM
2 Replies

5. Shell Programming and Scripting

create dir in main &subdirs,perform action

Hi, I have one dir which has N subdirs.For ex: /home/user/Project_Src /home/user/Project_Src/Dir_A /home/user/Project_Src/Dir_A/subdir/sub_dir2 /home/user/Project_Src/Dir_A/subdir/sub_dir3 /home/user/Project_Src/Dir_B /home/user/Project_Src/Dir_B/Build i want to create a folder with... (2 Replies)
Discussion started by: dragon.1431
2 Replies

6. Shell Programming and Scripting

Calculate total space, total used space and total free space in filesystem names matching keyword

Good afternoon! Im new at scripting and Im trying to write a script to calculate total space, total used space and total free space in filesystem names matching a keyword (in this one we will use keyword virginia). Please dont be mean or harsh, like I said Im new and trying my best. Scripting... (4 Replies)
Discussion started by: bigben1220
4 Replies

7. Shell Programming and Scripting

substrings from all files incl subdirs into csv with dir names

Greetings! I have multiple files, one per subdirectory, all with the same file name. All subdirectories are one level deep from the main directory. The data in the files is tab delimited between fields and record delimited with a newline. The subdirectory names have the date in the... (5 Replies)
Discussion started by: vtischuk@yahoo.
5 Replies

8. Shell Programming and Scripting

sftp how to deal with space in path to dir?

OK I am trying to use something similar to this: #!/bin/sh echo "OK, starting now..." sftp -b /dev/fd/0 user@remotehost <<EOF cd pub ascii put filename.txt bye EOF only difference is the dir I need to cd to has a space in it like this /Import/Server/Prospect File ,... (3 Replies)
Discussion started by: NewSolarisAdmin
3 Replies

9. AIX

find command - no subdirs

I am looking to delete files that are of a certain age with something like the following... find /directory -type f -mtime +14 | xargs rm ....however, I would like to only execute this on the current directory and not subdirectories. Any ideas? (4 Replies)
Discussion started by: andrewsc
4 Replies

10. Shell Programming and Scripting

calculate the space

Hi everyone, I need to write a script to calculate the space for sub-folders under /home: Here is the scanrio: cd /home drwxr-xr-x 57 root root 8192 Jan 22 16:13 home_1 drwxrwxrwx 69 root root 8192 Jan 29 10:36 home_2 drwxr-xr-x 97 root root 8192 Nov... (8 Replies)
Discussion started by: za_7565
8 Replies
Login or Register to Ask a Question