flis00
flis00

Reputation: 37

bash find command with path as requirement

I want to get file names of files in /bin that contain letter 'm' using find command not beeing in /bin. When /bin is my working directory it works fine but when I add /bin as requirement in path it returns nothing independently of current directory.

Works:

find -type f -name "*m*" -exec basename {} \;

Doesn't:

find -type f -name "*m*" -path "/bin/*" -exec basename {} \;

Upvotes: 1

Views: 1499

Answers (2)

gyan roy
gyan roy

Reputation: 101

This will work.

find /bin/* -type f -name "*m*" -exec basename {} \;

It is equivalent to going to /bin folder and executing

find -type f -name "*m*" -exec basename {} \;

Upvotes: 1

kojiro
kojiro

Reputation: 77107

I suspect you don't want to use -path /bin… but just

find /bin -type f -name "*m*" -exec basename {} \;

The first argument to find is the path to search in. The -path flag is a pattern matching feature that checks if the pattern matches the full path of the found name.

In fact, if you had tried this command on a BSD find such as comes with macOS, it won't even let you try one of your commands, because you didn't include the path.

find -type f …       # not ok
find . -type f …     # ok
find /bin -type f …  # ok

Upvotes: 2

Related Questions