Skip to content

Latest commit

 

History

History
90 lines (59 loc) · 1.78 KB

111-the-basename-command.md

File metadata and controls

90 lines (59 loc) · 1.78 KB

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

  1. 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$
  1. The basename command 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.txt and /etc/spam/eggs.docx you 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