how to use basename command

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
kaykav

how to use basename command

Post 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...
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29459
Joined: Wed Jul 06, 2011 3:58 am

Re: how to use basename command

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

Re: how to use basename command

Post by kaykav »

Ok thanks for that . I thought I was missing the jist of the command. Thanks again
pgmer6809
Level 4
Level 4
Posts: 228
Joined: Sun Mar 04, 2012 9:06 pm

Re: how to use basename command

Post 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
Locked

Return to “Scripts & Bash”