Last Updated : 18 Jul, 2025
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 is 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 the 'basename' commandbasename [path] [suffix]
where,
The 'basename' command is used for extracting the file name.
Syntax:basename /home/user/filename.txt
In case 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 of.
Syntax:basename /home/user/filenmame.txt .txtOptions for basename Command
The following basename
command options are specific to the GNU coreutils version, commonly found on most Linux distributions.
basename [OPTION]... NAME [SUFFIX]
where,
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.
Syntax:
basename -a /path/filename1.txt /anotherpath/filename2.logOption 2: '-s, - -suffix = SUFFIX'
This option removes a trailing suffix SUFFIX,such as a file extension.
Syntax:
basename - .txt /path/filename.txtOption 3: '-z' option:
This separates the output with NULL rather than a newline.
Syntax:
basename -z /path/filename.txtOption 4: '--help':
This command help to prints a message explaining the usage of the command and available options.
Syntax:
basename --helpOption 5: '--version'
It shows the version information of the basename
command.
Syntax:
basename --versionPurpose of the command "basename"
The basename
command is a lightweight yet highly useful tool in Linux for working with file paths. It is used to retrieve only the filename portion from a complete path and can also remove a specified suffix like a file extension. This functionality is particularly handy in shell scripting, where accurate and tidy handling of file names is essential. By separating the filename from its directory path, basename
improves the clarity, adaptability, and effectiveness of scripts that involve file processing.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4