How can I compress all the directories that do not contain at least one file used in the last month?

link|improve this question
3  
Please define "used" here. Changed, or just viewed/opened? – haimg Dec 11 '11 at 15:45
feedback

migrated from stackoverflow.com Dec 11 '11 at 12:59

This question came from our site for professional and enthusiast programmers.

1 Answer

You need a two-step approach. But why do you really want to do this in shell? It will be a lot faster when you actually implement this directly, say with a python script. The reason is that you can stop scanning a directory if you have found a "recent" file (except for subdirectories that you might still want to archive).

The naive shell-based approach would be to find all folders, then rescan each folder for the recent files. Something like this (for zsh)

find /start -depth -type d -print | while read dir; do
  files=$( find $dir -type f -mtime -30 )
  if test -z "$files"; then
    echo "Unused dir: $dir"
  fi
done

But again, note that this is not very efficient, and will also contain redundant entries (if a directory is "unused", it will also list all subdirs.)

link|improve this answer
+1 for the correct shell solution, but don't agree about a Python script being so much an improvement over Shell in the common case (at least for this task). If you really need extra performance, I can see you jumping instead to C. Perl/Python etc more powerful scripting langs IMO fill the niche of what is impractical to do with Shells because these won't directly support some feature, like some GUI, better data structures, floating-point arithmetic, or whatever (this talking from the perspective of common administration tasks that Shells were made programable exactly to cope with). – Juaco Dec 11 '11 at 14:33
You assume "used" == "modified" here, but the original poster does not say that explicitly. I certainly consider a file being used if I just, say, open it and print it, without saving... – haimg Dec 11 '11 at 15:44
@haimg: Well, substitute with ctime or atime, whichever you prefer. @Juaco: pythons os.path.walk is excellent, and makes this task very easy to solve in python. And after all, we are talking about lists and trees here, neither of which is an easy to use data structure in shell scripting. – Anony-Mousse Dec 11 '11 at 15:55
yeah i now it's better in python but i need to tho this in shell bash. but i need to compress this directory with the – Jhon Andromeda Dec 11 '11 at 18:20
feedback

Your Answer

 
or
required, but never shown