Operations 5 min read

Mastering the Linux basename Command: Quick Tips and Real-World Examples

This guide explains how to use the Linux basename utility to strip directory paths and suffixes from filenames, covering its basic syntax, useful options like -z, -a, and -s, handling multiple files, removing extensions, and embedding it in shell scripts.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Mastering the Linux basename Command: Quick Tips and Real-World Examples

The basename command is a small but essential command‑line tool that removes directory components and optional suffixes from a given filename, making it handy for scripting and file‑name manipulation.

Basic Syntax

basename NAME [SUFFIX]
basename OPTION... NAME...

When invoked with a single NAME, it prints the final pathname component. If a SUFFIX is supplied, that suffix is stripped from the result.

Simple Examples

[root@localhost ~]# basename /etc/yum.repos.d/CentOS-Base.repo
CentOS-Base.repo

By default the command also removes any trailing / characters:

[root@localhost ~]# basename /usr/local/
local
[root@localhost ~]# basename /usr/local
local

Useful Options

-z

(or --zero) – terminates each output line with a NUL character instead of a newline. -a (or --multiple) – allows processing multiple filenames in one call. -s (or --suffix=SUFFIX) – explicitly defines the suffix to remove.

Zero‑terminated output

[root@localhost ~]# basename -z /usr/local
local␀

Processing multiple files

[root@localhost ~]# basename -a /etc/passwd /etc/shadow
passwd
shadow

Removing a specific suffix

[root@localhost ~]# basename /etc/hostname name
host
[root@localhost ~]# basename -s name /etc/hostname
host

This technique is commonly used to strip file extensions:

[root@localhost ~]# basename -s .conf /etc/httpd/conf/httpd.conf
httpd
# or
[root@localhost ~]# basename /etc/httpd/conf/httpd.conf .conf
httpd

Combining -a and -s

[root@localhost ~]# basename -a -s .conf /etc/sysctl.conf /etc/httpd/conf/httpd.conf
sysctl
httpd

Embedding in Shell Scripts

The command is often used inside loops to rename batches of files. The example below converts all .jpg images in the current directory to .jpeg by renaming them with basename:

#!/bin/bash
for file in *.jpg
do
  mv "$file" "$(basename $file .jpg).jpeg"
done

Conclusion

The basename utility provides a straightforward way to extract filenames, drop directory paths, and remove unwanted suffixes, with options for handling multiple inputs and zero‑terminated output, making it a versatile tool for everyday Linux shell scripting.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxShell scriptingcommand-linebasenamefile-manipulation
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.