Tell me more ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question

2 Answers

up vote 5 down vote accepted

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.

share|improve this answer
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? – PeanutsMonkey Jul 22 '11 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 Jul 22 '11 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 Jul 22 '11 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? – PeanutsMonkey Jul 22 '11 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 – PeanutsMonkey Jul 22 '11 at 20:27
show 2 more comments

-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
share|improve this answer
/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? – PeanutsMonkey Jul 22 '11 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 Jul 22 '11 at 20:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.