1

The man page states that find -name refers to base of file name however I don't follow what that means exactly.

2 Answers 2

5

The "base of the filename" is the last part of the file path: The same thing you would get if you ran the basename(1) command. Typically this is everything after the last /.
This is as opposed to the dirname - the directory portion, or everything up to (but not including) the last /.
Together these make up the pathname of the file.

See the man page for basename (and dirname) for more details.

7
  • Thanks voretaq7. I had a read of basename and dirname which either output the prefix or suffix of a string. To clarify if I use -name it strips out the prefix of the string I am looking for e.g. if I were to search find / -name 'pwd', the output returns /bin/pwd. what is it stripping from the string? Commented Jul 22, 2011 at 20:06
  • The behavior is to avoid something like find /var -name foo from returning /var/foo, /var/foo/bar, /var/foo/baz, etc. By operating on the last part of the path (the basename) it avoids grabbing all the stuff under a matching directory that probably isn't what you're looking for.
    – voretaq7
    Commented Jul 22, 2011 at 20:10
  • For an equally contrived but more real-world example, find / -name bin will find /bin, /usr/bin, /usr/local/bin and maybe a few other spots. If it operated on the full pathname instead of the basename you'd also get /bin/ls, /bin/bash, and everything else in those directories: A few hundred files that probably aren't what you wanted.
    – voretaq7
    Commented Jul 22, 2011 at 20:14
  • Thanks. Sorry if I am being a complete noob. To clarify, if I were to run the command find / -name 'pwd', it is stripping out the / directory and returning only the absolute path i.e. /bin/pwd. Is that right? I am equally confused between the use of basename and full pathname. For me a full pathname is the absolute path e.g. /bin/pwd and the basename is pwd. Is that correct? Commented Jul 22, 2011 at 20:21
  • When you say If it operated on the full pathname instead of the basename you'd also get /bin/ls, /bin/bash, and everything else in those directories, I take you mean that if I were to run the command as follows; find / bin Commented Jul 22, 2011 at 20:27
2

-name is the string you are searching for. eg:

# find /etc/ -name passwd
/etc/pam.d/passwd
/etc/passwd

 

# find /usr/bin -name firefox
/usr/bin/firefox
2
  • /jscott - Thanks Daniel Baker/jscott. What happens if I omit -name and simply enter the string I am looking for e.g. find /etc/ 'passwd'. How is this any different? Commented Jul 22, 2011 at 19:56
  • @PeanutsMonkey - -name is a search predicate (it tells find what to look for). With no explicit predicate find treats its arguments as places to look (so find /etc/ passwd means "Look in /etc/ and ./passwd and tell me what you find.) -- Depending on which flavor of Linux you have The FreeBSD man page for find (with examples) may be more helpful: freebsd.org/cgi/… for more info
    – voretaq7
    Commented Jul 22, 2011 at 20:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .