Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading