Page 1 of 1

how to use basename command

Posted: Fri Dec 14, 2012 12:22 pm
by kaykav
I found a command 'basename'. Man pages are most always confusing. As an example I type... basename <path>. As a result I get the last file name of the path. So now what? What do I do with this result?
I fail to see the use of this command. It tells you what to do but not what to do with the result. Anyone explain? Thank you...

Re: how to use basename command

Posted: Fri Dec 14, 2012 12:31 pm
by xenopeek
basename gives you just the file name for a given argument. It's mostly used in scripts where for example you get a list of full paths from some command and you only want to use the file names.

Re: how to use basename command

Posted: Fri Dec 14, 2012 1:26 pm
by kaykav
Ok thanks for that . I thought I was missing the jist of the command. Thanks again

Re: how to use basename command

Posted: Wed Jan 02, 2013 12:12 am
by pgmer6809
The complement of basename is dirname.
so

Code: Select all

 basename /usr/share/progname 
returns the result progname

Code: Select all

dirname  /usr/share/progname
returns the result /usr/share without the trailing slash.

Code: Select all

 dirname /usr
returns /

Code: Select all

dirname   myMusic/
returns .

Code: Select all

 basename myMusic/
returns myMusic