Linux basename Command: Strip Directory and Suffix from File Names

By 

Updated on

3 min read

Using the Linux basename command to strip directory paths from file names

basename is a command-line utility that strips the leading directory path and an optional trailing suffix from a given file name, printing only the last component.

This guide explains how to use the basename command with practical examples.

Syntax

basename supports two syntax forms:

txt
basename NAME [SUFFIX]
basename OPTION... NAME...

The first form takes a single name and an optional suffix to strip. The second form, used with -a, accepts multiple names at once.

Basic Usage

The most common use is to strip the directory path from a full file path:

Terminal
basename /etc/passwd
output
passwd

basename also removes any trailing / characters, so both of the following produce the same result:

Terminal
basename /usr/local/
basename /usr/local
output
local
local

Multiple Inputs

Use the -a (--multiple) option to process several paths at once, separated by spaces:

Terminal
basename -a /etc/passwd /etc/shadow
output
passwd
shadow

This is equivalent to running basename separately on each path and collecting the output.

Remove a Trailing Suffix

Pass the suffix as a second argument to strip it from the result:

Terminal
basename /etc/sysctl.conf .conf
output
sysctl

The same result can be achieved with the -s (--suffix=SUFFIX) option:

Terminal
basename -s .conf /etc/sysctl.conf
output
sysctl

The -s form is required when stripping a suffix from multiple names with -a:

Terminal
basename -a -s .conf /etc/sysctl.conf /etc/sudo.conf
output
sysctl
sudo

NUL-Terminated Output

By default, each result ends with a newline. Use the -z (--zero) option to end lines with a NUL character instead of a newline. This is useful when passing output to tools that handle NUL-delimited input, such as xargs -0:

Terminal
basename -az -s .conf /etc/sysctl.conf /etc/sudo.conf | xargs -0 echo
output
sysctl sudo

Rename Files in a Script

The following example shows how to use basename inside a Bash for loop to rename files in the current directory, replacing the .jpeg extension with .jpg:

sh
for file in *.jpeg; do
    mv -- "$file" "$(basename "$file" .jpeg).jpg"
done

For bulk file renaming with more advanced pattern matching, the rename command is often a more concise alternative.

Quick Reference

TaskCommand
Strip directory from pathbasename /path/to/file
Strip directory and suffixbasename /path/to/file.txt .txt
Strip suffix with -sbasename -s .txt /path/to/file.txt
Process multiple pathsbasename -a /path/one /path/two
Strip suffix from multiple pathsbasename -a -s .conf /etc/a.conf /etc/b.conf
NUL-terminated outputbasename -z /path/to/file

FAQ

What is the difference between basename and dirname?
basename prints the last component of a path (the file or directory name). dirname prints everything except the last component (the parent directory path). They are complementary — running both on the same path reconstructs the original.

Does basename modify any files?
No. basename only parses the string you pass to it and prints the result. It does not read from or write to the filesystem.

Can basename strip any suffix, not just file extensions?
Yes. The suffix is a plain string match against the end of the name. basename report_final.txt _final.txt produces report. The suffix must appear at the very end of the name to be removed.

How do I strip a file extension in a Bash script without calling basename?
Bash parameter expansion can do this without a subprocess: ${filename%.*} strips the shortest trailing suffix matching .*. This is faster in loops because it avoids forking a new process for each file.

Conclusion

The basename command strips the directory path and optional suffix from a file name. It is most commonly used in shell scripts to extract file names from full paths or to remove extensions before renaming.

If you have any questions, feel free to leave a comment below.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page