I have several directories with useless files (like *.tmp
, desktop.ini
, Thumbs.db
, .picasa.ini
).
How to scan all drives to find directories which contain nothing but some of those files?
find
I have several directories with useless files (like *.tmp
, desktop.ini
, Thumbs.db
, .picasa.ini
).
How to scan all drives to find directories which contain nothing but some of those files?
Best Answer
To find all directories that contain no other name than
*.tmp
,desktop.ini
,Thumbs.db
, and/or.picasa.ini
:This would use
find
to locate any directories beneath the current directory (including the current directory) and pass them to a shell script.The shell script iterates over the given directory paths, and for each, it expands
*
in it (with thedotglob
shell option set inbash
to catch hidden names).It then goes through the list of resulting names and matches them against the particular patterns and names that we'd like to find (ignoring directories). If it finds any other name that doesn't match our list, it sets
ok
tofalse
(from having beentrue
) and breaks out of that inner loop.The
seen_files
variable becomestrue
as soon as we've seen a file of any type other than directory (or symlink to directory). This variable helps us avoid reporting subdirectories that only contain other subdirectories.It then runs
$seen_files
and$ok
(true
orfalse
) and if these are bothtrue
, which means that the directory contains at least one regular file, and only contains filenames in our list, it prints the pathname of the directory.Instead of
you could obviously do
instead.
Testing:
(
find
command is run here, producing the output...)This means that the directory
./dir
only contains names in the list (ignoring directories), while./dir/dir
contains other things as well.If you remove
[ -d "$name" ] && continue
from the code, the./dir
directory would not have been found since it contains a name (dir
) that is not in our list.