Quick take: basename /path/to/file.txt returns file.txt, and dirname /path/to/file.txt returns /path/to. Add a suffix to basename (basename file.txt .txt) to strip the extension. Both are staples of shell scripting.

Introduction

When a script receives a full path but needs just the filename or just the directory, basename and dirname do the job. basename strips the directory part (and optionally an extension), while dirname strips the filename — together they let scripts manipulate paths reliably.

Syntax

The basic syntax of the basename and dirname command is:

basename PATH [SUFFIX]
dirname PATH

Common Options and Parameters

The most useful options and parameters for the basename and dirname command:

OptionDescription
basename PATHStrip the directory, leaving the final component.
basename PATH SUFFIXAlso remove a trailing suffix (e.g. .txt).
basename -s .EXT *Strip a suffix from multiple arguments.
dirname PATHStrip the final component, leaving the directory.
basename -aTreat every argument as a path to process.

Practical Examples

Real basename and dirname commands you can run today:

# Get the filename from a path
basename /var/log/app.log
# Strip the extension too
basename /var/log/app.log .log
# Get the directory of a path
dirname /var/log/app.log
# Use both in a script
f=/etc/nginx/nginx.conf; echo "$(dirname $f) -> $(basename $f)"
# Loop over files using basename to rename
for f in *.jpeg; do mv "$f" "$(basename "$f" .jpeg).jpg"; done

Tips and Best Practices

  • Use basename file .ext to drop an extension — the classic way to build an output name from an input name in scripts.
  • Modern bash can do the same with parameter expansion (${f##*/} for basename, ${f%/*} for dirname), which is faster in loops.
  • Always quote the path (basename "$f") so filenames with spaces are handled correctly.

Final Thoughts

basename and dirname are the path-splitting workhorses of shell scripting — one keeps the filename, the other the directory, and basename can strip an extension on the way. Learn them for readable scripts, and the parameter-expansion equivalents (${f##*/}, ${f%/*}) for speed in tight loops.

FAQ: basename and dirname Command in Linux

How do I extract the filename from a path?+

Use basename: basename /var/log/app.log returns app.log. Add a suffix like basename /var/log/app.log .log to also strip the extension, giving app.

How do I get the directory part of a path?+

Use dirname: dirname /var/log/app.log returns /var/log. It strips the final component, leaving the containing directory.

How do I strip a file extension in bash?+

Use basename with the suffix (basename file.txt .txt) or parameter expansion: ${file%.txt} removes the .txt ending. Both yield the name without the extension.

What is the bash equivalent of basename and dirname?+

Parameter expansion: ${path##*/} acts like basename and ${path%/*} acts like dirname. They run inside the shell without spawning a process, so they are faster in loops.

Why should I quote paths with basename?+

Quoting (basename "$f") ensures filenames containing spaces or special characters are treated as a single argument, preventing errors in scripts.

Need help with Linux servers or infrastructure?

Work directly with Muhammad Irfan Aslam for Linux, Ubuntu, Docker, DevOps, cloud, CI/CD, or infrastructure support.

Hire Me for Support