Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux basename Command

Linux basename Command

Linux Command Reference Linux Command Reference


What is the basename command

basename is a simple but practical command-line tool in Linux used to extract the filename part from a file path. It is like a path parser that strips directory information and keeps only the final filename.

An Analogy: Imagine you have a complete mailing address (such as "China/Beijing City/Haidian District/No. 1 Zhongguancun Street"),basename function is to extract only the final "No. 1 Zhongguancun Street" part.


Basic syntax of the basename command



basename [选项] 路径 [后缀]

Parameter Description

  1. Path(required): The complete path string to process
  2. Postfix(optional): If specified, this suffix is removed from the result

Common options

Option Description
-a Supports multiple paths as arguments
-s 后缀 Remove the specified suffix (equivalent to adding a suffix argument at the end of the command)
-z Separate output results with a null character (instead of a newline)

Basic Usage Example

Example 1: Basic filename extraction

Example

$

basename

/

home

/

user

/

documents

/

report.txt

report.txt

Example 2: Remove a file extension

Example

$

basename

/

home

/

user

/

documents

/

report.txt .txt

report

Example 3: Process multiple files (using the -a option)

Example

$

basename

-a

/

path

/

to

/

file1.txt

/

another

/

path

/

file2.log

file1.txt

file2.log


Practical Application Scenarios

Scenario 1: Batch-process file extensions

Example

for

file

in

.jpg;

do

   

mv

"$file"

"$(basename "$file" .jpg).png"

done

Explanation: This script renames all .jpg files in the current directory to .png files, changing only the extension and keeping the original filename.

Scenario 2: Get the current script name in a script

Example

#!/bin/bash

SCRIPT_NAME

=$

(

basename

"$0"

)

echo

"Running script: $SCRIPT_NAME"

Scenario 3: Use with the find command

Example

find

/

var

/

log

-name

"*.log"

-exec

basename

{

}

;

Purpose: Find all .log files under /var/log and display only the filenames (without paths)


Frequently Asked Questions

Q1: What is the difference between basename and dirname?

Command Function Example input Example output
basename Extract the last part of a path /a/b/c.txt c.txt
dirname Extract the directory part /a/b/c.txt /a/b

Q2: Why does my basename command not work?

Common causes:

  1. A path contains special characters (such as spaces) but is not quoted

  2. Error:basename /path/with spaces/file
  3. Correct:basename "/path/with spaces/file"
  4. - **Wrong**: `basename /path/with spaces/file` - **Correct**: `basename "/path/with spaces/file"`
    1. A variable used in a script is not quoted

    2. Error:basename $filepath
    3. Correct:basename "$filepath"
    4. - **Wrong**: `basename $filepath` - **Correct**: `basename "$filepath"`

      Advanced Tips

      Tip 1: Handle multiple suffixes

      Example

      $

      basename

      archive.tar.gz .gz

      archive.tar

      $

      basename

      archive.tar.gz .tar.gz

      archive

      Tip 2: Use in a pipeline

      Example

      echo

      "/usr/local/bin/python"

      |

      xargs

      basename

      Output:python

      Tip 3: An alternative using parameter expansion

      In Bash, you can also use parameter expansion to achieve similar functionality:

      Example

      path

      =

      "/home/user/file.txt"

      filename

      =

      "${path##*/}"

       

      Equivalent to basename

      echo

      "$filename"


      Linux Command Reference Linux Command Reference