0

Suppose my directory structure looks like

A--file1.cpp
   file2.cpp
   file3.cpp
   file1.h
   file2.h
   file3.h

B--file1.cpp
   file2.cpp
   file3.cpp
   file1.h
   file2.h
   file3.h

and my goal is to find every cpp file besides file1.cpp in the A folder

find A/ B/ -name \*.cpp will find all the files, and I tried find A/ B/ ! -name file1.cpp -name \*.cpp this will exclude file1.cpp from the B folder as well. find A/ B/ -prune -o file1.cpp -name \*.cpp doesn't work either. What is the correct way to do it?

1 Answer 1

2

The -path predicate will let you ignore a specific file or directory.

find . -path ./A/file1.cpp -prune -o -type f -name '*.cpp' -print

Tested as follows:

tempdir=$(mktemp -d "$TMPDIR"/test.d.XXXXXX)
cd "$tempdir" && {
  mkdir -p A B
  touch {A,B}/file{1,2,3}.{cpp,h}
  find A B -path A/file1.cpp -prune -o -type f -name '*.cpp' -print
  rm -rf -- "$tempdir"
}

...emitting output of:

A/file2.cpp
A/file3.cpp
B/file1.cpp
B/file2.cpp
B/file3.cpp
Sign up to request clarification or add additional context in comments.

8 Comments

I tried find A/ B/ -path ./A/file1.cpp -prune -o -type f -print and it's showing all 6 files
I've amended with test procedure and output. You can copy-and-paste that yourself.
Your -path predicate needs to write the name in accordance with the path argument you passed to find telling it where/how to start the search. Thus, if you pass find the directory name A, it's A/file1.cpp.
It works, but I have .h files in the folder as well, so my problem might be too simplified, let me edit it and I will appreciate an updated answer
Amended appropriately.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.