Logo
  • Ubuntu
  • CentOS
  • Debian
  • Fedora
  • RedHat

Basename Command in Linux - DesignLinux

designlinux 0 Comments

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:

local
local

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
name

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.

If you have any questions or feedback, feel free to leave a comment.

basename terminal

Related

Tags: basename, terminal

How to Install Go in Ubuntu 20.04

Prev Post

How to Install Microsoft Teams on Linux

Next Post
Archives
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • July 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
Categories
  • AlmaLinux
  • Android
  • Ansible
  • Apache
  • Arch Linux
  • AWS
  • Backups
  • Bash Shell
  • Bodhi Linux
  • CentOS
  • CentOS Stream
  • Chef
  • Cloud Software
  • CMS
  • Commandline Tools
  • Control Panels
  • CouchDB
  • Data Recovery Tools
  • Databases
  • Debian
  • Deepin Linux
  • Desktops
  • Development Tools
  • Docker
  • Download Managers
  • Drupal
  • Editors
  • Elementary OS
  • Encryption Tools
  • Fedora
  • Firewalls
  • FreeBSD
  • FTP
  • GIMP
  • Git
  • Hadoop
  • HAProxy
  • Java
  • Jenkins
  • Joomla
  • Kali Linux
  • KDE
  • Kubernetes
  • KVM
  • Laravel
  • Let's Encrypt
  • LFCA
  • Linux Certifications
  • Linux Commands
  • Linux Desktop
  • Linux Distros
  • Linux IDE
  • Linux Mint
  • Linux Talks
  • Lubuntu
  • LXC
  • Mail Server
  • Manjaro
  • MariaDB
  • MongoDB
  • Monitoring Tools
  • MySQL
  • Network
  • Networking Commands
  • NFS
  • Nginx
  • Nodejs
  • NTP
  • Open Source
  • OpenSUSE
  • Oracle Linux
  • Package Managers
  • Pentoo
  • PHP
  • Podman
  • Postfix Mail Server
  • PostgreSQL
  • Python
  • Questions
  • RedHat
  • Redis Server
  • Rocky Linux
  • Security
  • Shell Scripting
  • SQLite
  • SSH
  • Storage
  • Suse
  • Terminals
  • Text Editors
  • Top Tools
  • Torrent Clients
  • Tutorial
  • Ubuntu
  • Udemy Courses
  • Uncategorized
  • VirtualBox
  • Virtualization
  • VMware
  • VPN
  • VSCode Editor
  • Web Browsers
  • Web Design
  • Web Hosting
  • Web Servers
  • Webmin
  • Windows
  • Windows Subsystem
  • WordPress
  • Zabbix
  • Zentyal
  • Zorin OS
Visits
  • 0
  • 433
  • 574,923

DesignLinux.com © All rights reserved

Go to mobile version