Home > File & Directory Management > basename-u

basename-u: Extracting Filenames (basename command)

This document addresses the 'basename-u' command. However, 'basename-u' does not exist in the standard Linux command list. Assuming the user either mistyped 'basename' or is referring to a non-standard script/alias used in a specific environment, this guide provides instructions for the standard 'basename' command. 'basename' is used to remove the directory portion from a given path and extract the pure filename or directory name.

Overview

The 'basename' command is used to remove the directory component from a file path, leaving only the filename (or the last directory name). This is very useful when manipulating file paths in shell scripts, especially when only the file's name is needed.

Key Features

  • Extracts the filename from a file path
  • Optionally removes a specified suffix from the filename
  • Provides batch processing for multiple paths

Key Options

Here are the main options available in the standard 'basename' command.

Basic Operation and Filtering

Generated command:

Try combining the commands.

Description:

`basename-u` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Here are practical usage examples of the standard 'basename' command.

Extracting Basic Filename

basename /home/user/documents/report.pdf

Extracts only the filename from a given path.

Removing a Suffix

basename /var/log/syslog.log .log

Removes a specific suffix from the filename.

Extracting Multiple Filenames

basename -a /etc/hosts /usr/bin/ls /tmp/test.txt

Extracts filenames from multiple paths at once.

Using Null Termination with xargs

find . -maxdepth 1 -type f -print0 | xargs -0 basename -z

Safely processes null-terminated output using `xargs -0`.

Tips & Notes

Explanation of the '-u' option in basename-u

The '-u' included in the command name is not a valid option for the standard 'basename' command. GNU Coreutils' 'basename' command does not have a '-u' option, and using it may result in an error message like 'basename: invalid option -- 'u''. If '-u' is used in a specific script or environment, it is likely not the 'basename' command itself but rather a custom script or alias.

Combination with dirname

'basename' extracts only the filename, while 'dirname' extracts only the directory path. Using these two commands together allows for the complete separation of a file path into its directory and filename components, which is very useful when writing shell scripts.

  • Example: path="/home/user/documents/report.pdf" echo "Directory: $(dirname "$path")" echo "Filename: $(basename "$path")"

Same category commands