Find directories with all files inside older than X?
Solution 1
It's probably possible to do this without creating files using process substitution or something, but here's a quick-and-dirty solution:
find . -type f -mtime +30 -printf '%h\n' | sort | uniq > old.txt
find . -type f -mtime -30 -printf '%h\n' | sort | uniq > new.txt
grep -vf new.txt old.txt
The first command outputs the path of every file modified more than 30 days ago (in find's -printf
-- at least with the GNU find on my system -- %h
prints the whole path except for the actual filename), then sorts those and gets rid of any duplicates, and puts the whole thing into a file called old.txt
.
The second command does the same but with every file modified less than 30 days ago, and puts them into another file, new.txt
.
The grep line prints every line from old.txt that doesn't appear in new.txt -- so it will give you a list of directories that contain only files that were last modified more than 30 days ago.
This is all using the GNU versions of the utilities. I don't know if the syntax matches up on the BSD versions, etc.
Solution 2
Finally figured out the magic one-liner:
for dir in `find . -type d -mtime +30`; do test `find $dir -type f -mtime -30 -print -quit` || echo $dir; done
This prints any directories that have a modification time greater than 30 days and no files modified within the last 30 days.
Solution 3
This one should work:
for DIR in {the folder list}; do
if [[ $(find ${DIR} -mtime -10 -type f) == "" ]]; then
echo ${DIR}
fi
done
It finds all directories in {the folder list}, that contains files older than 10 days.
Basically what find ${DIR} -mtime -10 -type f
does is to find all files (-type f
) that are modified within 10 days (-mtime -10
, which is different from -atime
or -ctime
, see here for more information: http://www.cyberciti.biz/faq/howto-finding-files-by-date/). If such files are not found within a directory, then we know that this directory is older than 10 days, and we output this directory with echo ${DIR}
.
Hope it helps!
Related videos on Youtube
naught101
Contact: Skype: naught101 XMPP/Googletalk: [email protected] [my username] at gmail.com
Updated on September 18, 2022Comments
-
naught101 almost 2 years
Is it possible on linux to find directories where all contained files and directories (and sub-directories' files etc.) are older than a given age? In other words, if a directory has one or more files within that have a modification date more recent than a given threshold, that directory should not be listed. If all of the files and folders below that directory are older than the given threshold, then that directory should be listed.
The use case is that my home directory is full of hidden directories, and I'm sure that many of them are left overs from previous installations, and software that I haven't used in years. I'd like to be able to find these directories, so I can easily decide whether to cull them.
-
cYrus about 12 yearsWhat do you mean by "older": modification or access time? Note that the latter will be meaningless if the file system is mounted with
relatime
ornoatime
. Also a very old modification time doesn't necessarily mean that a file is useless.
-
-
naught101 about 12 yearsYes, I know this. I'm not looking for files older than the date, I'm looking for directories which only contain files and directories which are all older than the given date.
-
naught101 about 12 yearsSee my comment on Petkaux answer. This doesn't solve my question, and it's also a bit nutty, Why wouldn't I just use
find -newert 201206010100
? -
scriptmonster about 12 yearsyou could use -d option on touch command which will enable you to create file at a given date.
-
naught101 about 12 yearsBefore you spend more time on this answer, I think you should re-read the question. It is not asking what you think it is asking.
-
naught101 over 10 yearsNice. That's a neat solution!
-
shonky linux user over 10 yearsgrep can be slow and memory intensive if the directory listing is huge. In this case you could use comm instead. Refer superuser.com/questions/409493/…
-
naught101 over 8 yearsNice. That is very cool.
-
MariusMatutiae almost 8 yearsWhen you suggest a solution to a problem, it is most helpful, also for future readers, to explain in some detail why your solution works. The key point here is the use of the
-mtime
option offind
: which units does it use (minutes,hours,days), how does it differ fromatime
, thin kind of things. -
username123 almost 8 years@MariusMatutiae Thanks for your advice, I've updated the answer.