how to use basename command

Forum rules
Before you post please read how to get help
kaykav
Level 3
Level 3
Posts: 148
Joined: Tue Jan 06, 2009 12:09 am
Location: crossville tn

how to use basename command

Postby kaykav » Fri Dec 14, 2012 12:22 pm

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...

User avatar
xenopeek
Level 23
Level 23
Posts: 19380
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: how to use basename command

Postby xenopeek » Fri Dec 14, 2012 12:31 pm

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.
Image

kaykav
Level 3
Level 3
Posts: 148
Joined: Tue Jan 06, 2009 12:09 am
Location: crossville tn

Re: how to use basename command

Postby kaykav » Fri Dec 14, 2012 1:26 pm

Ok thanks for that . I thought I was missing the jist of the command. Thanks again

pgmer6809
Level 3
Level 3
Posts: 120
Joined: Sun Mar 04, 2012 9:06 pm

Re: how to use basename command

Postby pgmer6809 » Wed Jan 02, 2013 12:12 am

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


Return to “Scripts & Bash”

Who is online

Users browsing this forum: No registered users and 1 guest