[email protected]Office hours: Mon – Fri 9:00AM – 5:00PMLinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedAugust 14, 2023

How to use basename command.

How To Use Basename Command.

basename is a command-line utility that strips directory and trailing suffix from given file names.

 

Using the basename Command

The basename command supports two syntax formats:

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

basename takes a filename and prints the last component of the filename. Optionally, it can also remove any trailing suffix. It is a simple command that accepts only a few options.

The most basic example is to print the file name with the leading directories removed:

basename /etc/passwd

The output will include the file name:

passwd

The basename command removes any trailing / characters:

basename /usr/local/basename /usr/local

Both commands will produce the same output:

basename

By default, each output line ends in a newline character. To end the lines with NUL, use the -z (–zero) option.

 

Multiple Inputs

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/passwd and /etc/shadow you would run:

basename -a /etc/passwd /etc/shadow
passwd
shadow

 

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:

basename /etc/sysctl.conf .conf
sysctl

Another way to remove a trailing suffix is to specify the suffix with the -s (–suffix=SUFFIX) option:

basename -s .conf /etc/sysctl.conf
sysctl

This syntax form allows you to strip any trailing suffix from multiple names:

basename -a -s .conf /etc/sysctl.conf /etc/sudo.conf
sysctl
sudo

 

Example

The following example shows how to use the basename command inside a bash for loop to rename all files ending with “.jpeg” in the current directory by replacing the file extension from “.jpeg” to “.jpg”:

for file in *.jpeg; do
    mv -- "$file" "$(basename $file .jpeg).jpg"
done

If you are using bash as your shell, instead of invoking basename, you can use strip the trailing extension using Shell Parameter Expansion .

 

Conclusion.

The basename command strips any leading directory and trailing suffix from the name.

Keep reading
How to Implement Network Segmentation and Resource Isolation in Linux.
February 6, 2024

How to Implement Network Segmentation and Resource Isolation in Linux.

How to Implement Network Segmentation and Resource Isolation in Linux. Securing and managing networks in a Linux environment involves advanced practices like network segmentation and resource isolation. This guide explores these concepts, providing detailed instructions and examples for…

Read article
How to open or close ports in Linux.
February 6, 2024

How to open or close ports in Linux.

How to open or close ports in Linux. Effectively managing ports on your Linux system is crucial for both security and service accessibility. This comprehensive guide will provide a detailed walkthrough on opening and closing ports using the robust tools iptables and ufw. Understanding these…

Read article
How to use ypdomainname command.
January 23, 2024

How to use ypdomainname command.

How to use ypdomainname command. In the realm of Linux networking, the ypdomainname command plays a crucial role in the context of the Network Information Service (NIS). NIS, formerly known as Yellow Pages, is a distributed database service that facilitates the sharing of network configuration and…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, colocation, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Office hours: Mon – Fri 9:00AM – 5:00PM