I need to add a script which will delete files from directory before installation of new version. I need save from directory one catalog – /logs and all files inside ( inside we have *.log and *.zg files)
Ive created this line : find /directory/path/* ! -name 'logs' -type d,f -exec rm -r -v {} +
But in Debian 11 Its cleaning also my files inside of catalog log.
Do you know what can be a reason ?
It works on zsh on macbook m1 and is not cleaning my log catalog.
Take Care : )
Expectation
bash script which delete all catalogs and files from given directory EXCEPT one catalog /log and all files inside ( inside we have *.log and *.zg files) .
2
Answers
Consider:
The issue here is that although the directory
logs
matches the primary-name logs
, the namea
inlogs
does not. So find operates on that file. Rather than negating the-name
primary, you want to match thelogs
directory andprune
that from the find:Also note that instead of
find /directory/path/* ...
, you almost certainly wantfind /directory/path ...
. There’s no need to pass all of the names in/directory/path
as arguments tofind
, and indeed that is … weird. Just pass the top level dir and letfind
handle the tree descent.Maybe extended globbing.