Stylized image in shades of orange of a woman sitting in front of a laptop.

How to use the basename command

0

The basename command is a GNU core utility for UNIX and UNIX-like systems, and is designed to remove the file suffix and leading directories from a filename. It’s a reasonable alternative to at least some uses of the cut command. You’re not likely to use it alone, but it’s an interesting tool to have available to you when constructing a complex command.

The simplest use for basename is retrieving just the name of a file, regardless of its path

$ basename ~/Documents/Work/Example/my_file.pdf
my_file.pdf

This makes parsing a full path a lot easier than rev and cut or awk alternatives. I’ve used basename when searching for standardised file names across various storage devices. For instance, this command search my storage device for files called reference-chart.webp:

$ find /storage -name `basename ~/Assets/reference-chart.webp`

It makes more sense when it’s part of a loop or script:

for FILE in ~/Assets/* ;
  do find /storage -name `basename $FILE` ; 
  done

Get multiple filenames with basename

You can get the basename of several files with the --multiple (or -a for short) option.

$ basename ~/Documents/my_file.odt ~/Documents/example.txt 
my_file.odt
example.txt

The output is not sorted by default. The return values are in the order you provided. The --zero (-z for short) eliminates the newline, and returns the results with no break between them:

$ basename --zero ~/Documents/my_file.odt ~/Documents/example.txt 
my_file.odtexample.txt

Drop a suffix with basename

The --suffix (-s for short) option for basename excludes the file extension from the command’s result. To drop the .txt extension from a file:

$ basename --suffix .txt ~/Documents/example.txt
example

Alternately, you can just provide the extension after the command:

$ basename ~/Documents/example.txt .txt
example

In practise, this is more useful when it’s part of a larger command. For example, I have a script that converts images from one format to another, according to a specified naming convention.
The destination name is dependent upon the existing project, however, the script first establishes the base format of the name, and then converts the image accordingly. Here’s a simplified version of the command without all the preamble:

convert ~/Promo/12.png \
~/Project/`basename -s .jpg ~/Project/project_080124.jpg`.webp

Basename is a parsing command

The basename command is meant for basic parsing. It’s essentially a string function for the shell. There are many other ways to get the same results as basename, often with less knowledge about the expected results. It’s probably a command we could all live without, but it’s still a handy option to have when faced with a problem. Sometimes, the easiest option is the one that seems the simplest.

There’s a lot of simplicity in basename, but it can be pleasantly powerful when you find the right job for it.

Leave a Reply