Open In App

basename Command in Linux with examples

Last Updated : 18 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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' command

basename [path] [suffix]

where,

  • Path: Refers to the location of a file or directory in the filesystem. It shows how to navigate from the root(/) or current directory to the target file.
  • Suffix: Suffix refers to a specific string (usually a file extension) that you want to remove from the end of the filename.

Example 1: To extract the file name.

The 'basename' command is used for extracting the file name.

Syntax:

basename /home/user/filename.txt
basename

Example 2: To extract the file name without the suffix.

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 .txt
b1

Options for basename Command

The following basename command options are specific to the GNU coreutils version, commonly found on most Linux distributions.

Syntax:

basename [OPTION]... NAME [SUFFIX]

where,

  • OPTION: refers to the options compatible with the
  • NAME: refers to the file name or file full pathname

Option 1: '-a, - -multiple':

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.log
other

Option 2: '-s, - -suffix = SUFFIX'

This option removes a trailing suffix SUFFIX,such as a file extension.

Syntax:

basename - .txt /path/filename.txt
other1

Option 3: '-z' option:

This separates the output with NULL rather than a newline.

Syntax:

basename -z /path/filename.txt
base2

Option 4: '--help':

This command help to prints a message explaining the usage of the command and available options.

Syntax:

basename --help
base3

Option 5: '--version'

It shows the version information of the basename command.

Syntax:

basename --version
base4

Purpose of the command "basename"

  • Displays just the file name portion of a full directory path.
  • Helps isolate the file name from its full path for easier processing.
  • Facilitates path manipulation in shell scripts by returning only the filename component.
  • Commonly used in shell scripts to extract and manage file names from full paths.

Use Cases of the Command 'basename'

  • Extracting filenames from full file paths within scripts
  • Stripping file extensions to work with base filenames
  • Presenting clean, simplified output by omitting directory paths

Conclusion:

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.


Practice Tags :

Similar Reads