basename strips directory information and suffixes from file names i.e. it prints the file name NAME with any leading directory components removed.
Using basename command :
The basename command can be easily used by simply writing basename followed by the file name or the full pathname.
Syntax of basename command :
$basename NAME [SUFFIX] or $basename OPTION NAME
where, NAME refers to the file name or file full pathname and OPTION refers to the options compatible with the basename command and in case, if you want to strip off the suffix of a file you can give the filename followed by the SUFFIX name you want to get rid off.
/* basename removes directory path and returns the file name, kt */ $basename /usr/local/bin/kt kt
In the first case, the basename command removes the directories name from the full path name given of file kt and in the second case basename removes the suffix .html from kt.html.
Options for basename command :
- -a, – -multiple option : This option lets you support multiple arguments and treat each as a NAME i.e you can give multiple file names or full path names with the use of -a option.
/* -a is used for multiple inputs */ $basename -a /usr/local/bin/kt /usr/local/bin/kt.html kt kt.html
- -s, – -suffix = SUFFIX option : This option removes a trailing suffix SUFFIX,such as a file extension.
/*.html suffix is removed from kt.html when followed by -s option */ $basename -s .html kt.html kt
- -z option : This separates the output with NULL rather than a newline.
/* The outputs of the two arguments passed are not separated by a newline*/ $basename -az /usr/local/bin/kt kt.html ktkt
- – -help option : This displays the help information and exit.
- – -version option : This displays the version information and exit.
Application of basename command :
There are many utilities available which are only useful when it comes to shell scripts and doesn’t have any practical application when used by itself and basename comes under this category.
Example of a shell script which converts gif image files into png image files with the help of basename command :
/*shell script to convert the gif image file format into png image file format */ #!/bin/sh for file in *.gif;do #exit if there are no files if [! -f $file];then exit fi b='basename $file .gif' echo NOW $b.gif is $b.png giftopnm $b.gif | pnmtopng >$b.png done
In the above shell script basename command is used to extract the file names with extension .gif without the extension so that they can be converted into .png files.
References : basename command
- df command in Linux with Examples
- du command in Linux with examples
- col command in Linux with Examples
- tac command in Linux with Examples
- bc command in Linux with examples
- cd command in Linux with Examples
- let command in Linux with Examples
- cp command in Linux with examples
- dc command in Linux with examples
- arp command in Linux with examples
- sum command in Linux with Examples
- apm Command in Linux With Examples
- gdb command in Linux with examples
- pwd command in Linux with Examples
- ZIP command in Linux with examples
- tty command in Linux with examples
- ip command in Linux with examples
- until command in Linux with Examples
- pv command in Linux with Examples
- apt-get command in Linux with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.