Home > Net >  bash find command with path as requirement
bash find command with path as requirement

Time:02-25

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 {} \;

CodePudding user response:

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

CodePudding user response:

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 {} \;
  • Related