basename

file managementlinux
The basename command is one of the most frequently used commands in Linux/Unix-like operating systems. basename The basename command strips directory and suffix from filenames. It is used to extract just the filename from a path or to remove a suffix from a filename.

Quick Reference

Command Name:

basename

Category:

file management

Platform:

linux

Basic Usage:

basename [options] [arguments]

Common Use Cases

    Syntax

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

    Options

    Option Description
    -a, --multiple Support multiple arguments and treat each as a NAME (GNU version)
    -s, --suffix=SUFFIX Remove a trailing SUFFIX; implies -a (GNU version)
    -z, --zero End each output line with NUL, not newline (GNU version)
    --help Display help information and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the basename command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    #

    Basic Examples:

    # Extract filename from a path
    basename /usr/local/bin/example.txt
    # Output: example.txt
    # Remove extension from a filename basename example.txt .txt # Output: example
    # Extract filename from a path with trailing slash basename /usr/local/bin/ # Output: bin
    # Process multiple files (GNU version only) basename -a /path/file1.txt /path/file2.txt # Output: file1.txt file2.txt

    Advanced Examples:

    # Use in a shell script to process filenames
    for file in /var/log/*.log; do
      name=$(basename "$file")
      echo "Processing $name"
    done
    # Remove any suffix (not just extension) basename myfile.backup .backup # Output: myfile
    # Multiple suffix handling basename myfile.tar.gz .tar.gz # Output: myfile # Use with dirname to get both path components path="/home/user/documents/report.pdf" dir=$(dirname "$path") file=$(basename "$path") echo "Directory: $dir, File: $file" # Output: Directory: /home/user/documents, File: report.pdf

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    Purpose and Functionality:

    The basename command serves a simple but essential purpose in shell scripting and path handling:

    • It extracts the final component from a pathname, removing any leading directory components.
    • If specified, it also removes a trailing suffix from the filename.
    • It's commonly used in scripts to process filenames without their directory paths.
    • basename is complementary to the dirname command, which extracts the directory portion of a path.

    Implementation Notes:

    • basename considers a trailing slash (/) to refer to a directory, and returns the last directory name.
    • The SUFFIX is removed only if it matches exactly at the end of the NAME.
    • If NAME consists solely of slashes, basename will return a single slash.
    • basename ignores any non-existent or inaccessible pathnames - it operates purely on the string.
    • basename always treats its input as a string, not as an actual file path that needs to exist.

    Variations Between Implementations:

    • GNU basename (common on Linux systems) includes additional options like -a for multiple arguments.
    • BSD and POSIX versions may have fewer options and slightly different behavior with edge cases.
    • Some shells like bash include a built-in basename functionality that can be faster than the external command.
    • In bash, you can use parameter expansion: ${filename##*/} to get the basename of $filename.
    • Similarly, in bash ${filename%.*} removes the extension from a filename.

    Common Use Cases:

    • Shell scripts that process files and need to work with just the filenames.
    • Creating output files with names based on input files but with different extensions.
    • Log file naming based on script names (e.g., $(basename "$0").log).
    • Displaying user-friendly filenames without their full paths.

    Related Commands

    These commands are frequently used alongside basename or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the basename command works in different scenarios.

    $ basename
    View All Commands