jcdole
September 21, 2017, 6:16am
1
Hello.
For a given folder, I want to select any files
find $PATH1 -f \( -name "*"
but omit any files like pattern name
! -iname "*.jpg" ! -iname "*.xsession*" ..... \)
and also omit any subfolder like pattern name
-type d \( -name "/etc/gconf/gconf.*" -o -name "*cache*" -o -name "*Cache*" -o -name ".mozilla" -o -name ".googleearth" \) -prune
My last try is :
find "$PATH1" -type f \( -name "*" ! -iname "*~" ! -iname "*.iso" ! -iname "*.tar" ! -iname "*.bz2" ! -iname "*.gz"
! -iname "*.tgz" ! -iname "*.7z" ! -iname "*.sfx" ! -iname "*.zip" ! -iname "*.rpm"
! -iname "*.gz.aa" ! -iname "*.rpm" ! -iname "*.pdf" ! -iname "*.png" ! -iname "*.jpg" ! -iname "*.dll"
! -iname "*.exe" ! -iname "*.xsession*" \) -type d \( -name "/etc/gconf/gconf.*" -o -name ".cache"
-o -name ".Cache" -o -name ".mozilla" -o -name ".googleearth" \) -prune -o -print
I still got files ".cache*" from ".mozilla", ".Cache*" from googleearth, ".xsession-errors" and ".xsession-errors-:0"
By the way I am not sure that the pattern
! -iname "*.xsession*"
is a good pattern for
/home/some_user/.xsession-errors
Filtering subfolders works :
find "$PATH1" -type d \( -name "/etc/gconf/gconf.*" -o -name ".cache"
-o -name ".Cache" -o -name ".mozilla" -o -name ".googleearth" \) -prune -o -print
How to add file patterns exclusion.
Any help is welcome
Maintaining something like that is hard - as you found out.
Consider splitting up the lists, make a file with the exclusion patterns, one pattern per line.
Call it exclusions. Changes in red.
Example:
find "$PATH1" -type f \( -name "*" ! -iname "*~" ! -iname "*.iso" ! -iname "*.tar" ! -iname "*.bz2" ! -iname "*.gz"
! -iname "*.tgz" ! -iname "*.7z" ! -iname "*.sfx" ! -iname "*.zip" ! -iname "*.rpm"
! -iname "*.gz.aa" ! -iname "*.rpm" ! -iname "*.pdf" ! -iname "*.png" ! -iname "*.jpg" ! -iname "*.dll"
! -iname "*.exe" ! -iname "*.xsession*" \) -a -type d \( -name "/etc/gconf/gconf.*" -o -name ".cache"
-o -name ".Cache" -o -name ".mozilla" -o -name ".googleearth" \) -prune -o -print | grep -v -f $HOME/exclusions > resultfile
In order to test exclusions you can feed it output from a directory that had oddball file names
ls -a /path/to/somedir | grep -f $HOME/exclusions # note: no -v option
By default
echo *
does not match filenames that start with a dot.
So you might need ! -iname "*.xession* ! -iname ".xsession*"
But not needed if you exclude all .*
by requiring -name *
.
To not descend into directories, I think you need -o
instead of the -a
in the last post. But how to fiddle the -print
in the middle?
It is more comfortable to first exclude the directories.
find "$PATH1" \
-type d \( -path "/etc/gconf/gconf.*" -o -name ".[Cc]ache" -o -name ".mozilla" -o -name ".googleearth" \) -prune -o \
-type f -name "*" ! -iname "*~" ! -iname "*.iso" ! -iname "*.tar" ! -iname "*.bz2" ! -iname "*.gz" ! -iname "*.tgz" \
! -iname "*.7z" ! -iname "*.sfx" ! -iname "*.zip" ! -iname "*.rpm" ! -iname "*.gz.aa" ! -iname "*.pdf" ! -iname "*.png" \
! -iname "*.jpg" ! -iname "*.dll" ! -iname "*.exe" ! -iname "*.xsession*" -print
The explicit -print
is needed, otherwise it will default-print the pruned directory names.