Bash get basename of filename or directory name nixCraft

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##*/}"

Bash Get Basename of Filename or Directory Name on Linux and Unix

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() ##
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

Extract file basename without path and extension in bash

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 ]
then 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 ] then 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 fi


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.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Facebook Comments
Spread the love

Posted by News Monkey