How to Use the Linux basename Command to Strip File Paths

When writing bash scripts in Linux to automate backups or process large batches of files, you frequently need to isolate the actual name of a file from its long, complex directory path. For example, if a variable in your script contains the string /var/www/html/website/assets/images/logo-final-v2.png, and you just want to print “logo-final-v2.png” to a log file, manually attempting to slice the string using complex awk or sed regex commands is tedious and error-prone. The Linux basename command is a simple, dedicated utility designed to strip away the directory path and optionally remove the file extension, returning only the core filename.

Stripping the Directory Path

The primary function of basename is to read a full, absolute path and return only the final component.

  1. Open your terminal application.
  2. Type the command followed by the full file path: basename /var/log/nginx/access.log
  3. Press Enter.

The terminal will instantly output: access.log. It completely ignores all the preceding directory slashes.

It is important to note that the basename command is entirely a string manipulation tool. It does not check if the file actually exists on the hard drive. You can pass it absolute nonsense like basename /fake/path/to/unicorn.txt, and it will happily output unicorn.txt.

Stripping File Extensions (Suffixes)

The second powerful feature of basename is its ability to remove the file extension (the suffix) from the output. This is incredibly useful in scripting. For instance, if your script finds a file named video.mp4 and needs to create a matching thumbnail named video.jpg, you must first strip the .mp4 extension to get the raw name “video”.

To strip a suffix, simply add the exact extension you want to remove as a second argument after the file path.

basename /home/user/downloads/video.mp4 .mp4

The output will be exactly: video.

Using the -s Flag for Multiple Files

If you need to process multiple paths at once, standard basename syntax becomes clunky. To process multiple files while simultaneously stripping their extensions, you must use the -s (suffix) flag, followed by the extension, and then list all your file paths.

basename -s .txt /path/file1.txt /path/file2.txt /path/file3.txt

The terminal will output the clean names on three separate lines:

file1
file2
file3

Common Bash Script Implementation

The most common way to use basename is by assigning its output to a variable inside a bash script using command substitution ($() or backticks). Here is a standard implementation pattern:

#!/bin/bash
FULL_PATH="/home/admin/backups/database_dump.tar.gz"

# Extract just the filename with the extension
FILE_NAME=$(basename "$FULL_PATH")
echo "Processing file: $FILE_NAME"

# Extract just the raw name without the extension
RAW_NAME=$(basename "$FULL_PATH" .tar.gz)
echo "The raw name is: $RAW_NAME"

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.