How to traverse directory structure and sum size of files?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to traverse directory structure and sum size of files?
# 1  
Old 06-24-2010
How to traverse directory structure and sum size of files?

How do I write a bash or ruby or perl or groovy script to print all the files in my directory tree that are one-to-two years old, the size of each file, and the sum of file sizes and then delete them?

I was using
Code:
find . -atime +365 -exec rm '{}' \;

but the problem was that I could not tell if it was hung because it took a very long time. There are a half million files in this directory tree. I can't figure out how to enhance this to give me a sum of the space freed up with the deletes and give me some updates as to what is going on so I know it is not hung.

How do I tell find to get files with in a set of date ranges?

Thanks,
Siegfried

Moderator's Comments:
Mod Comment Use code tags, ty.

Last edited by zaxxon; 06-25-2010 at 04:52 AM.. Reason: code tags
# 2  
Old 06-25-2010
Half a million files can take their time. Add a -print in front of your -exec to see if it's stuck.

---------- Post updated at 11:15 AM ---------- Previous update was at 09:54 AM ----------

You should also use the -mtime option instead of -atime since -atime timestamp will be updated even if you had just a reading action on the file. -mtime resembles to the date you see when you do a ls -l.
To express a "starting" and "ending" time you can also combine 2 -mtime like:
Code:
find . -mtime -730 -mtime +365 -print -exec rm '{}' \;

which should delete all files older than 1 year but not older than 2 years ie. the year before the last year.
When experimenting with this, leave out the -exec rm so you do not accidentally delete things if something is not according to your needs.

Last edited by zaxxon; 06-25-2010 at 06:15 AM.. Reason: added info
# 3  
Old 06-28-2010
Now how do I sum the sizes of the files as I delete them?
# 4  
Old 06-29-2010
You could simply run this with a ls -la instead of a rm in the -exec part at 1st and sum them up with shell arithmetics or awk...
After that run the same with rm. Or build a construct with pipe and xargs etc. to do both in one line.
# 5  
Old 06-29-2010
This may help in getting the file size of all the files

Code:
sum=0
for k in `find . -mtime -730 -mtime +365 -exec ls -l {} \+ | awk '{print $5}'`
do
   sum=$((sum+k))
done
echo "Total file size is $sum"


Last edited by radoulov; 06-29-2010 at 04:37 AM.. Reason: Please use code tags!
# 6  
Old 06-29-2010
Quote:
Originally Posted by sanjaypraj
Code:
sum=0
for k in `find . -mtime -730 -mtime +365 -exec ls -l {} \+ | awk '{print $5}'`
do
   sum=$((sum+k))
done
echo "Total file size is $sum"

Another untested version of the above.

Code:
find . -mtime -730 -mtime +365 -exec ls -l {} \+  | awk '{  x += $5  } END {print "Total file size is "x}'

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Archiving files keeping the same structure directory

Hello Team, We would like to backup a lot of files inside of a structure of directories, four, five or more levels in some Ubuntu, Mac and Solaris systems. For instance: /home/chuck/sales/virgin/rent-quote.pdf /home/chuck/sales/marriott/vacation-quote.pdf... (2 Replies)
Discussion started by: csierra
2 Replies

2. Shell Programming and Scripting

How to delete some of the files in the directory, if the directory size limits the specified size

To find the whole size of a particular directory i use "du -sk /dirname".. but after finding the direcory's size how do i make conditions like if the size of the dir is more than 1 GB i hav to delete some of the files inside the dir (0 Replies)
Discussion started by: shaal89
0 Replies

3. UNIX for Dummies Questions & Answers

How to get the set of files size as a sum in a directory.

Hi, Can someone help me to get the complete files size (sum) over a perod time (1 day,2days)in a directory. eg: Directory :ABC I have a1,a2,a3 files are created in last 24 hours so I need to get the some of all these files. I am using the below find command but its giving me the... (1 Reply)
Discussion started by: gaddamja
1 Replies

4. Shell Programming and Scripting

Traverse file structure from top and rename the immediate parent

Hello All, I am trying to write a script to:- 1. Traverse entire file system, look for directories which has .git directory in it 2. Rename the immediate parent directory to <orignal_name.git> 3. Traverse new file structure and look for all directories with <original_name>.git 4. cd to... (2 Replies)
Discussion started by: sahil_jammu
2 Replies

5. Shell Programming and Scripting

Sum of file size in directory / subdirectory

Hi , I am trying to write something to find the size of particular type of files in a directory & it's subdirectory and sum the size .. These types of file are found at directory level or its subdirectories level .. #!/bin/ksh FNAME='.pdf' S_PATH=/abc/def/xyz find $S_PATH -exec ls -lad... (4 Replies)
Discussion started by: Vaddadi
4 Replies

6. Shell Programming and Scripting

Sum up files whose size Kbytes and Mbytes

Hello Friends, When i type du -sh *.jar | sort -n under a library directory i get a result similar below output: 1M 1.jar 2.4M 2.jar 4.5M 3.jar . . . . . . 1K (n-2).jar 15K (n-1).jar 77.7K n.jar I want to sum up the size... (1 Reply)
Discussion started by: EAGL€
1 Replies

7. Shell Programming and Scripting

find size, cp breaks directory structure

I'm using this now: find /some/path/with/sourcefiles -type f -size -7M -exec /bin/cp -uv {} /some/path/ \; but it doesn't preserve the directory structure, also I've tried it with find /some/path/with/sourcefiles -type f -size -7M -exec /usr/bin/rsync -auv {} /some/path/ \; but that doesn't... (9 Replies)
Discussion started by: unclecameron
9 Replies

8. UNIX for Dummies Questions & Answers

copy files with directory structure

i have a text file as. /database/sp/NTR_Update_Imsi_List.sql /database/sp/NTR_Update_Imsi_Range_List.sql /database/sp/NTR_Vlr_Upload.sql /database/tables/StatsTables.sql /mib/ntr.mib /mib/ntr.v2.mib /scripts/operations/ntr/IMSITracer.ph /scripts/operations/ntr/IMSITracer.pl ... (3 Replies)
Discussion started by: adddy
3 Replies

9. UNIX for Advanced & Expert Users

MV files from one directory structure(multiple level) to other directory structure

Hi, I am trying to write a script that will move all the files from source directory structure(multiple levels might exist) to destination directory structure. If a sub folder is source doesnot exist in destination then I have to skip and goto next level. I also need to delete the files in... (4 Replies)
Discussion started by: srmadab
4 Replies
Login or Register to Ask a Question