The Wayback Machine - https://web.archive.org/web/20250305171255/https://www.geeksforgeeks.org/basename-command-in-linux-with-examples/
Open In App

basename Command in Linux with examples

Last Updated : 01 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The ‘basename’ command in Linux is a fundamental utility used in file manipulation and script writing. It simplifies file paths by stripping directory information and optional suffixes from file names. Here a detailed overview of the ‘basename’ command, including its syntax, options, and practical usage examples.

What is the ‘basename’ Command?

‘basename’ is used to extract the base file name from a given path by removing the path up to and including the last slash (‘/’). This command is particularly useful in scripting and programming where file names need to be handled separately from their directory paths.

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.

Example :

/* 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.html
    
  • ‘- -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


Master DevOps and also get 90% Course fee refund on completing 90% course in 90 days! Take the Three 90 Challenge today.

The most sought-after Three 90 challenge has started and this is your chance to upskill and get 90% refund. What more motivation do you need? Start the challenge right away!


Next Article

Similar Reads

Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a Linux and macOS cat command that improved its precursor: the syntax highlighted file contents can be displayed with its help. Therefore, in the case of source code and configuration files, it is extremely helpful. It can highlight over 10 languages, like JavaScript, Java, Ruby, Python, Go, C, and JSON.Syntaxccat [OPTIONS] [FILE...]Basic U
3 min read
How to Display Command History in Linux | history Command
The history command in Linux is one of the most essential tools especially for those who prefer working with the Linux terminal. This powerful command allows users to view, search, and manipulate the list of previously executed commands. Mastering the history command in Linux helps you efficiently reuse commands, automate repetitive tasks, and trou
3 min read
Tail command in Linux with examples
It is the complementary of head command. The tail command, as the name implies, prints the last N number of data of the given input. By default, it prints the last 10 lines of the specified files. If more than one file name is provided then data from each file is preceded by its file name. Syntax of Tail Command in Linuxtail [OPTION]... [FILE]... L
7 min read
wc command in Linux with examples
wc stands for word count. As the name implies, it is mainly used for counting purpose.It is used to find out number of lines, word count, byte and characters count in the files specified in the file arguments.By default it displays four-columnar output.First column shows number of lines present in a file specified, second column shows number of wor
6 min read
bc command in Linux with examples
bc command is used for command line calculator. It is similar to basic calculator by using which we can do basic mathematical calculations. Arithmetic operations are the most basic in any kind of programming language. Linux or Unix operating system provides the bc command and expr command for doing arithmetic calculations. You can use these command
11 min read
AWK command in Unix/Linux with examples
Awk is a scripting language used for manipulating data and generating reports. The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators. Awk is a utility that enables a programmer to write tiny but effective programs in the form of statements that def
8 min read
Paste command in Linux with examples
Paste command is one of the useful commands in Unix or Linux operating system. It is used to join files horizontally (parallel merging) by outputting lines consisting of lines from each file specified, separated by tab as delimiter, to the standard output. When no file is specified, or put dash ("-") instead of file name, paste reads from standard
6 min read
rm command in Linux with examples
rm stands for remove here. rm command is used to remove objects such as files, directories, symbolic links and so on from the file system like UNIX. To be more precise, rm removes references to objects from the filesystem, where those objects might have had multiple references (for example, a file with two different names). By default, it does not
5 min read
uname command in Linux with Examples
Linux, renowned for its open-source flexibility and powerful performance, offers a range of commands that reveal the inner workings of your system. Among these, the 'uname' command stands out as a versatile tool that provides key details about your Linux machine. Here, we will learn the basics of the 'uname' command, its various options, and how it
4 min read
cal command in Linux with Examples
The 'cal' command in Linux is a versatile tool that displays calendars directly in the terminal. If a user wants a quick view of the calendar in the Linux terminal, 'cal' is the command for you. Here’s a look at the usage and features of 'cal' command in Linux.What is the 'cal' command?cal command is a calendar command in Linux which is used to see
2 min read