Bash Wiki
Posts (Latest 30 updated) :
Read all
Contents:
  1. [Linux] Bash basename Usage: Extracting the filename from a path
    1. Overview
    2. Usage
    3. Common Options
    4. Common Examples
    5. Tips

[Linux] Bash basename Usage: Extracting the filename from a path

Overview

The basename command in Bash is used to strip the directory and suffix from filenames, returning just the filename portion. This is particularly useful when you need to extract the name of a file from its full path.

Usage

The basic syntax of the basename command is as follows:

basename [options] [arguments]

Common Options

  • -a: Treat each argument as a separate name and return the basename for each.
  • -s: Remove a specified suffix from the filename.
  • --help: Display help information about the command.

Common Examples

  1. Basic Usage Extract the filename from a full path:
    basename /home/user/documents/file.txt
    

    Output:

    file.txt
    
  2. Removing a Suffix Remove a specific suffix from the filename:
    basename /home/user/documents/file.txt .txt
    

    Output:

    file
    
  3. Multiple Filenames Get the basenames of multiple files:
    basename -a /home/user/documents/file1.txt /home/user/documents/file2.txt
    

    Output:

    file1.txt
    file2.txt
    
  4. Using with Variables Use basename with a variable:
    FILE_PATH="/home/user/documents/file.txt"
    basename "$FILE_PATH"
    

    Output:

    file.txt
    

Tips

  • Always quote your variables when using basename to avoid issues with spaces in filenames.
  • Use the -s option when you need to remove specific extensions from multiple files efficiently.
  • Combine basename with other commands like find or xargs for powerful file manipulation in scripts.