Bash get basename of filename or directory name

I need to extract file basename in bash running on Linux. How can I use bash to get basename of filename or directory name for given path?

Introduction: One can extract filename and extension in bash shell using built-in commands or external commands. The ‘$’ character introduces parameter expansion, command substitution, or arithmetic expansion. See “How To Use Bash Parameter Substitution Like A Pro” for more info. This page shows to remove file basename without path and extension in bash using various methods.

Bash Get Basename of Filename or Directory Name

To extract filename and extension in Bash use any one of the following method:

  1. basename /path/to/file.tar.gz .gz – Strip directory and suffix from filenames
  2. ${VAR%pattern} – Remove file extension
  3. ${VAR#pattern} – Delete from shortest front pattern

Let us see some example in bash to get basename of filename.

Bash get filename and extension

To just get filename from a given path:

basename "$FILE"
f="$(basename -- $FILE)"
echo "$f"

Get filename without using basename command

The syntax is:

echo ${FILE##*/}
## another example ##
echo "${url##*/}"

Get running script name using bash parameter

The $0 will provide full path for currently running script. To extract that name only:

echo "$_self is called"
## or in usage() ##
    echo "$_self: arg1 arg2"

Bash get a file extension

Try the following examples:

echo "${FILE#*.}"     # print tar.gz
echo "${FILE##*.}"    # print gz
ext="${FILE#*.}"      # store output in a shell variable 
echo "$FILE has $ext" # display it

How to get the basename from the full filename path in bash

Use any one of the following syntax:

basename $file 
echo ${file##*/}

Let us just get the first part of the $file when you know that .enc is extension:

basename $file .enc
## without using basename ##
echo "$t"

Script example to extract filename and extension in Bash

# Purpose: Compile latest Linux kernel
# Author: Vivek Gite {} 
# License: GPL version 2.0 or above
# ---------------------------------------------------------------- 
set -e
isdl=1 # do not download
current="$(uname -r)"
curl -s > "$_out"
url="$(grep -A 2 '<td id="latest_button">' ${_out}  | grep -Eo '(http|https)://[^/"]+.*xz')"
echo "* Current Linux kernel: $current"
echo "* Remote Linux kernel version: $remote"
[ "$current" = "$remote" ] || isdl=0
if [ $isdl = 0 ]
	notify-send "A new kernel version ($remote) has been released."
	echo "* Downloading new kernel ..."
	wget -qc "$url" -O "${dldir}/${file}"
	wget -qc "$gpgurl" -O "${dldir}/${gpgurl##*/}"
	echo "* Using gpg to verify new tar ball ..."
	cd "$dldir"
	xz -fd "$file"
        gpg --verify "${gpgurl##*/}"
	if [ $? -eq 0 ]
		notify-send "Now compiling kernel ver $remote..."
		tar xf "${file%.xz}"
		cd "${file%.tar.xz}"
		cp -v "/boot/config-$(uname -r)" .config
		make -j $(nproc) && /usr/bin/notify-send "Password needed to install new kernel..." && sudo make modules_install && sudo make install && sudo reboot


You just learned how to get the basename and extension from the full filename when using Bash shell running on a Linux or Unix-like system.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 0 comments... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum