3

enter image description hereI have written this script Scrit in Linux.

I now need to know the size for each sub directory with the condition -atime +${DAYS} searching down from directory $DIR. The result should be gathered in a text file $FILENAME.

#!/bin/bash
#Days ago to search
DAYS="1"
#Path to directory to be searched
DIR="/media/nss/MBVOL2/TEST1/"
#Result filename Solomon
FILENAME="/media/nss/MBVOL2/TEST1/solomon2.txt"
# Process of listing these files in a result text file 
find "${DIR}" -type f -atime +${DAYS} -print > $FILENAME
3
  • 1
    What does moving up the directory tree from $DIR to / have to do with the condition? Your question is quite unclear.
    – Anthon
    Mar 6, 2014 at 14:33
  • I need to to have list of files which have NOT been access for X days and calculate on a sub directory basis the size these files take.
    – user62104
    Mar 6, 2014 at 15:54
  • I have rewritten your question because it was quite unclear (hence the answers including the use of du, please roll back the edit if you don't agree with my edits
    – Anthon
    Mar 6, 2014 at 16:41

2 Answers 2

5

If you want the size of the subdirectories, use du. This will print the size of the subdirectories recursively (-h is for human readable sizes):

du -h /media/nss/MBVOL2/TEST1/

If you only want a summary and not each of the results, use -s:

du -sh /media/nss/MBVOL2/TEST1/

If you want the size of the directories regarding the atime condition of your script, you can use the following:

find -type d -atime +1 -exec du -sh {} \;

This will execute the command after exec (and before \;) and pass all the files found by the find command. However, bear in mind that you are actually accessing those files and directories, so the atime will be modified for them.

2
  • How do I capture the result in a another file?
    – user62104
    Mar 6, 2014 at 16:04
  • Just redirect the output: find -type d -atime +1 -exec du -sh {} \; > /tmp/whatever
    – Alvaro
    Mar 6, 2014 at 18:48
2

You can pipe find through du in the final line to include the file size. Something like:

find "${DIR}" -type f -atime +${DAYS} -print0 | xargs -0 du -csh '{}' > $FILENAME

should get you close.

3
  • 2
    That doesn't give the size of the directories, just the size of individual files. Mar 6, 2014 at 13:10
  • I have the result in a text file. But how do I obtain the size of each sub-directory up to root-directory? Thanks.
    – user62104
    Mar 7, 2014 at 20:47
  • I need my above script to sum up for each sub directory: meaning I need to know the total size for sub directory A-CV (line 20)then B-CV (line 37) then C-CV (line 59). Currently I do that with Excel but this not workable. Any idea?
    – user62104
    Apr 3, 2014 at 16:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .