
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:
- basename /path/to/file.tar.gz .gz – Strip directory and suffix from filenames
- ${VAR%pattern} – Remove file extension
- ${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:
FILE="/home/vivek/lighttpd.tar.gz" basename "$FILE" f="$(basename -- $FILE)" echo "$f" |
Get filename without using basename command
The syntax is:
FILE="/home/vivek/lighttpd.tar.gz" echo ${FILE##*/} ## another example ## url="https://www.cyberciti.biz/files/mastering-vi-vim.pdf" echo "${url##*/}" |
Get running script name using bash parameter
The $0 will provide full path for currently running script. To extract that name only:
#!/bin/bash _self="${0##*/}" echo "$_self is called" ## or in usage() ## usage(){ echo "$_self: arg1 arg2" } |
Bash get a file extension
Try the following examples:
FILE="/home/vivek/lighttpd.tar.gz" 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:
file="/home/vivek/.gpass/passwd.enc" basename $file echo ${file##*/} |
Let us just get the first part of the $file when you know that .enc is extension:
file="/home/vivek/.gpass/passwd.enc" basename $file .enc ## without using basename ## t="${t%.enc"} t="${t%.enc}" echo "$t" |
Script example to extract filename and extension in Bash
#!/bin/bash # Purpose: Compile latest Linux kernel # Author: Vivek Gite {https://www.cyberciti.biz/} # License: GPL version 2.0 or above # ---------------------------------------------------------------- set -e _out="/tmp/out.$$" dldir=~/linux isdl=1 # do not download current="$(uname -r)" curl -s https://www.kernel.org/ > "$_out" url="$(grep -A 2 '<td id="latest_button">' ${_out} | grep -Eo '(http|https)://[^/"]+.*xz')" gpgurl="${url/tar.xz/tar.sign}" file="${url##*/}" remote="${file%.tar.xz}" remote="${remote#linux-}" 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 fi |
Conclusion
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.