0

I have the following folder structure

| subdirectoy1
| |
| |_ file1.jpg
| |_ file2.jpg
| |_ ...
|
| subdirectory2
| |
| |_ file11.jpg
| |_ file12.jpg
| |_ ...
|
| ...
|
|_ file21.jpg
|_ file22.jpg
|_ ...

As you can see, there are some files at the root level of that folder and various subdirectories (more than the two in my tree here) with each of them containing more images.

My idea is now, that I want to find both the root level images and the images in subdirectory1, but not those in the other directories.

Well, I could go the easy way and just exclude the other directories one by one

find . -type f -not -path "*subdirectory2*" -exec ...

but I want this line to be more adaptable for cases when the other folders aren't named subdirectory2 and so on.

Or in other words: Is there a way to say

find . -maxdepth 1 ...

but with the exception of one specific subdirectory below that boundry?

  • Does it really have to be a single find? Looks like running find twice should solve that. – Eduardo Trápani Oct 30 '19 at 15:01
  • -not is not supported by find...never use it – schily Oct 30 '19 at 17:46
4

You can prune directories which aren’t subdirectory1:

find . ! \( -name . -o -name subdirectory1 \) -prune -type f

You can add whatever you want to do with the files after -type f.

This works as follows:

  • Starting in the current directory
    • prune anything which doesn’t match . or subdirectory1 (this will ignore any other directory)
    • keep only regular files

This will find all files in the current directory and in subdirectory1 (without descending into subdirectory1’s sub-directories).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.