Bootstrap FreeKB - Linux Commands - basename
Linux Commands - basename

Updated:   |  Linux Commands articles

The basename command extracts the everything after the last / character in a string. In this example, just the text "Documents" will be returned.

basename /home/john.doe/Documents

 

In this example, "foo.txt" will be returned.

basename /home/john.doe/foo.txt

 

The -s or --suffix option can be used to remove a suffix, typically a file extension, from the output. In this example, "foo" will be returned.

basename --suffix .txt /home/john.doe/foo.txt

 

The -a or --multiple option can be used to return multiple matches. In this example, foo.txt and bar.txt will be returned.

basename --multiple /home/john.doe/foo.txt /home/john.doe/bar.txt

 

basename can also be used in a shell script to print the name of the script. For example, if the script is foo.sh, the following command in the script will print foo.sh.

echo $(basename $0)

 

The pwd (present working directory) command can be used to return the absolute path leading up to the basename.

~]# pwd
/home/john.doe

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter c58e16 in the box below so that we can be sure you are a human.