skip to Main Content

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


  1. Consider:

    $ find . ! -name logs
    .
    ./logs/a
    ./foo
    ./foo/a
    

    The issue here is that although the directory logs matches the primary -name logs, the name a in logs does not. So find operates on that file. Rather than negating the -name primary, you want to match the logs directory and prune that from the find:

    $ find . -name logs -prune -o -print
    .
    ./foo
    ./foo/a
    

    Also note that instead of find /directory/path/* ..., you almost certainly want find /directory/path .... There’s no need to pass all of the names in /directory/path as arguments to find, and indeed that is … weird. Just pass the top level dir and let find handle the tree descent.

    Login or Signup to reply.
  2. Maybe extended globbing.

    shopt -s extglob
    rm -frv /directory/path/!(logs)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search