1.8 KiB
Executable File
The basename command
The basename is a command-line utility that strips directory from given file names. Optionally, it can also remove any trailing suffix. It is a simple command that accepts only a few options.
Examples
The most basic example is to print the file name with the leading directories removed:
basename /etc/bar/foo.txt
The output will include the file name:
foo.txt
If you run basename on a path string that points to a directory, you will get the last segment of the path. In this example, /etc/bar is a directory.
basename /etc/bar
Output
bar
The basename command removes any trailing / characters:
basename /etc/bar/foo.txt/
Output
foo.txt
Options
- By default, each output line ends in a newline character. To end the lines with NUL, use the -z (--zero) option.
$ basename -z /etc/bar/foo.txt
foo.txt$
- The
basenamecommand can accept multiple names as arguments. To do so, invoke the command with the-a(--multiple) option, followed by the list of files separated by space. For example, to get the file names of/etc/bar/foo.txtand/etc/spam/eggs.docxyou would run:
basename -a /etc/bar/foo.txt /etc/spam/eggs.docx
foo.txt
eggs.docx
Syntax
The basename command supports two syntax formats:
basename NAME [SUFFIX]
basename OPTION... NAME...
Additional functionalities
Removing a Trailing Suffix: To remove any trailing suffix from the file name, pass the suffix as a second argument:
basename /etc/hostname name
host
Generally, this feature is used to strip file extensions
Help Command
Run the following command to view the complete guide to basename command.
man basename