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
- ed command in Linux with examples
- rev command in Linux with Examples
- atq command in linux with examples
- apt-get command in Linux with Examples
- more command in Linux with Examples
- cpp command in Linux with Examples
- bc command in Linux with examples
- wc command in Linux with examples
- tty command in Linux with examples
- tar command in Linux with examples
- Cat command in Linux with examples
- until command in Linux with Examples
- let command in Linux with Examples
- atd command in Linux with examples
- for 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.