bash how to basename


Command basename strip directory and suffix from filenames. Command syntax:

basename [option] name [suffix]

If suffix is specified it will remove a trailing suffix. Example:

basename dir1/dir2/dir3/text_file.txt .txt

Output: text_file

Basename takes one argument (file name) and optional suffix. If you want to give more file names use option "a" which support multiple arguments and threat each as "name".

basename -a /dir/file.txt /dir2/picture.jpg

Output: file.txt picture.jpg

If you want to get the name of your home folder:

basename ~

Often used option is option -s which removes a trailing suffix. Here is example:

basename -s .txt -a /dir/file.txt /dir2/picture.jpg

file picture.jpg

Was this information helpful to you? You have the power to keep it alive.
Each donated € will be spent on running and expanding this page about UNIX Shell.