basename command in Linux with Examples



Name

basename - strip directory and suffix from filenames.

Synopsis

basename NAME [SUFFIX]
basename OPTION... NAME...

Description

Print NAME with any leading directory components removed from full pathname. If SUFFIX is specified and is identical to the end of NAME, it is removed from NAME as well. Note that since trailing slashes are removed prior to suffix matching, SUFFIX will do nothing if it contains slashes. basename prints the result on standard output.

Options

-a, --multiple
   support multiple arguments and treat each as a NAME

-s, --suffix=SUFFIX
   remove a trailing SUFFIX; implies -a

-z, --zero
   end each output line with NUL, not newline

--help display this help and exit

--version
   output version information and exit

Examples

1. By default, if you run the basename command with a full path to a file as an input, the command returns the filename in output. For example,

$ basename /usr/include/string.h 
string.h

2. When we run the same command with a SUFFIX .h it removes the SUFFIX from NAME.

$ basename /usr/include/string.h .h
string

3. We can use -a option to handle multiple inputs.

$ basename -a /home/expert/ /home/expert/unicode/ /usr/include/stdio.h 
expert
unicode
stdio.h    

4. .txt suffix is removed from all the .txt files listed below when basename command is followed by -s option. See the example below.

$ ls -l test*.txt
-rw-rw-r-- 1 expert expert   340 Jun  3 14:37 test-data-1.txt
-rw-rw-r-- 1 expert expert 11927 Jun  3 14:36 test-data-2.txt
-rw-rw-r-- 1 expert expert 35604 Feb 27  2020 test-data-input.txt
-rw-rw-r-- 1 expert expert  2339 Oct 10 13:36 test-file-in-hindi-10oct20.txt
-rw-rw-r-- 1 expert expert  2427 Oct 10 13:37 test-file-in-hindi-rev-10oct20.txt
$ basename -s .txt test*.txt
test-data-1
test-data-2
test-data-input
test-file-in-hindi-10oct20
test-file-in-hindi-rev-10oct20

5. Use -s option to output a zero byte (ASCII NUL) at the end of each line, rather than a newline. This option enables other programs to parse the output even when that output would contain data with embedded newlines.

$ basename -z -s .txt test*.txt
test-data-1test-data-2test-data-inputtest-file-in-hindi-10oct20test-file-in-hindi-rev-10oct20$

6. We can use od command to see that after each filename listed in the above result there is a NULL byte. This ASCII NULL byte can be used by other programs to parse the output.

$ basename -z -s .txt test*.txt|od -bc
0000000 164 145 163 164 055 144 141 164 141 055 061 000 164 145 163 164
          t   e   s   t   -   d   a   t   a   -   1  \0   t   e   s   t
0000020 055 144 141 164 141 055 062 000 164 145 163 164 055 144 141 164
          -   d   a   t   a   -   2  \0   t   e   s   t   -   d   a   t
0000040 141 055 151 156 160 165 164 000 164 145 163 164 055 146 151 154
          a   -   i   n   p   u   t  \0   t   e   s   t   -   f   i   l
0000060 145 055 151 156 055 150 151 156 144 151 055 061 060 157 143 164
          e   -   i   n   -   h   i   n   d   i   -   1   0   o   c   t
0000100 062 060 000 164 145 163 164 055 146 151 154 145 055 151 156 055
          2   0  \0   t   e   s   t   -   f   i   l   e   -   i   n   -
0000120 150 151 156 144 151 055 162 145 166 055 061 060 157 143 164 062
          h   i   n   d   i   -   r   e   v   -   1   0   o   c   t   2
0000140 060 000
          0  \0
0000142
$ 
Advertisements