Linux basename Command: Strip Directory and Suffix 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:
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:
basename /etc/passwdpasswdbasename also removes any trailing / characters, so both of the following produce the same result:
basename /usr/local/
basename /usr/locallocal
localMultiple Inputs
Use the -a (--multiple) option to process several paths at once, separated by spaces:
basename -a /etc/passwd /etc/shadowpasswd
shadowThis 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:
basename /etc/sysctl.conf .confsysctlThe same result can be achieved with the -s (--suffix=SUFFIX) option:
basename -s .conf /etc/sysctl.confsysctlThe -s form is required when stripping a suffix from multiple names with -a:
basename -a -s .conf /etc/sysctl.conf /etc/sudo.confsysctl
sudoNUL-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:
basename -az -s .conf /etc/sysctl.conf /etc/sudo.conf | xargs -0 echosysctl sudoRename 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:
for file in *.jpeg; do
mv -- "$file" "$(basename "$file" .jpeg).jpg"
doneFor bulk file renaming with more advanced pattern matching, the rename command
is often a more concise alternative.
Quick Reference
| Task | Command |
|---|---|
| Strip directory from path | basename /path/to/file |
| Strip directory and suffix | basename /path/to/file.txt .txt |
Strip suffix with -s | basename -s .txt /path/to/file.txt |
| Process multiple paths | basename -a /path/one /path/two |
| Strip suffix from multiple paths | basename -a -s .conf /etc/a.conf /etc/b.conf |
| NUL-terminated output | basename -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.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

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