Home > Net >  find: Exclude files with the same name based on their depth in a directory
find: Exclude files with the same name based on their depth in a directory

Time:05-20

I have a free of files like this:

root_dir
|
-- dir1
     |
     -- file.ext
     -- misc.txt
     subdir
        |
        -- file.ext
        -- rest.ext
|
-- dir2
     |
     -- file.ext
     -- misc.txt
     subdir
        |
        -- file.ext
        -- rest.ext

and I want to compress the following subset of it:

root_dir
|
-- dir1
     |
     -- misc.txt
     subdir
        |
        -- file.ext
        -- rest.ext
|
-- dir2
     |
     -- misc.txt
     subdir
        |
        -- file.ext
        -- rest.ext

My plan is to first get a list of the files, and then pipe them to gzip or tar. The files in dir1 and dir2 have the same name (but different content). Yet, the problematic part is that file.ext in dir* and subdir have the same name, which makes filtering the files based on the names a bit tricky.

Following this, I have tried the following in the root_dir

find -type -f -not -name *misc.txt*

but, expectedly, it finds the file.ext at the depth one as well. Is there a way to exclude these depth-1 file.exts from the find ouput?

Notes

  • the extentions are dummy and represent a bunch of files with various types
  • I have hundreds of dir1 and dir2
  • each dir* is very large and its not efficient to compress all first, and then remove the first level file.exts

CodePudding user response:

find . -type f -not -regex '\./[^/] /file\.ext'

It will discard anything that match the regex. Here a direct subdirectory containing the file file.ext.

The regex is run to the whole path.

  • \. means starts with a .
  • [^/] means a bunch of characters not containing a /
  • file\.ext means file.ext
  • Related