1

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 {} \;
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
flis00
  • 37
  • 6
  • Being able to include only predicates and actions and no paths in a `find` command is a GNU extension. The POSIX standard makes inclusion of one or more starting paths mandatory. – Charles Duffy Feb 24 '22 at 20:47
  • That said -- which version of `find` _is_ this? If it's the GNU one in fact, you can use `-printf` and not need `basename` (the set of format strings includes an option to print the filename only). – Charles Duffy Feb 24 '22 at 20:48
  • Also, note that `find` **is not part of bash**, so this question was originally tagged incorrectly; you would have the exact same behavior on zsh, or dash, or ksh, or any other shell, as long as it was used to run the same version of `find` you have here. – Charles Duffy Feb 24 '22 at 20:49

2 Answers2

2

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
kojiro
  • 74,557
  • 19
  • 143
  • 201
  • find /bin returns noting as well – flis00 Feb 24 '22 at 20:44
  • @flis00, which OS is this? If `/bin` is a symlink to `/usr/bin` (as it is on GNU Arch, f/e), you can end in some interesting situations. That'll also tell us which version of `find` you have. – Charles Duffy Feb 24 '22 at 20:49
  • ubuntu 20.04 in virtualbox. /bin actually has files and #!/bin/bash works as well. find (GNU findutils) 4.7.0 – flis00 Feb 24 '22 at 21:04
  • 1
    Does `(cd -P /bin && find . …)` work? (I know you said in the question you wanted to use find _not_ in /bin, but I'm curious if `cd -P` resolves to a different path.) – kojiro Feb 24 '22 at 22:10
  • @flis00, how did you test if `/bin` "actually has files"? If you ran `cd /bin; ls`, that just tells you `/bin` _evaluates to a location_ that has files, not that it is a directory that contains files itself: it could be a symlink, which you need to _follow_ to get to where the files are. – Charles Duffy Feb 25 '22 at 14:39
1

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 {} \;
gyan roy
  • 101
  • 5
  • Why `/bin/*` and not just `/bin`? You're expanding _every file in `/bin`_ into `find`'s command line before running it; if there are too many files to fit on one command line, it won't run at all (and since command line arguments and environment variables use the same pool of memory, the more/larger variables you `export`, the shorter of a command line it takes to get an error). – Charles Duffy Feb 25 '22 at 14:39