Bash get filename from given path on Linux or Unix

How do I extract filename and extension in the Bash shell script from the given path? How can I get filename from the path under the bash shell?

It is possible to get just the filename from a path in a bash shell script running on a Linux or Unix-like systems. One can use any one of the following methods to extract filename or extension in bash.

Bash get filename from given path

The basename command strip directory and suffix from filenames. The syntax is pretty simple:
basename /path/to/file
basename /path/to/file suffix

Examples

Let us see how to extract a filename from given file such as /bin/ls. Type the following basename command:
basename /bin/ls
You can pass multiple arguments using the -a option as follows:
basename -a /bin/date /sbin/useradd /nas04/nfs/nixcraft/data.tar.gz
Store outputs in a shell variable, run:

out="$(basename /data/backup-file.tar.gz)"
echo "Filename is $out"

How to remove extesnion from filenames

Remove .gz from backups.tar.gz file and get backups.tar only:
basename /backups/14-nov-2019/backups.tar.gz .gz
OR

basename -s .gz /backups/14-nov-2019/backups.tar.gz
#
# Bash get filename from path and store in $my_name variable 
#
my_name="$(basename -s .gz /backups/14-nov-2019/backups.tar.gz)"
echo "Filename without .gz extension : $my_name"


Extract filename and extension in Bash

From the bash man page:

The ‘$’ character introduces parameter expansion, command substitution, or arithmetic expansion. The parameter name or symbol to be expanded may be enclosed in braces, which are optional but serve to protect the variable to be expanded from characters immediately following it which could be interpreted as part of the name. When braces are used, the matching ending brace is the first }’ not escaped by a backslash or within a quoted string, and not within an embedded arithmetic expansion, command substitution, or parameter expansion. The basic form of parameter expansion is ${parameter}. The value of parameter is substituted.

How to extract filename and extension in bash shell script

For instance, define a shell variable named $input. Let us create a shell script named test.sh:

#!/bin/bash
input="/home/vivek/work/config.ini"
# extract config.ini
file_name="${input##*/}"
# get .ini 
file_extension="${file_name##*.}"
# get config 
file="${file_name%.*}"
# print it
echo "Full input file : $input"
echo "Filename only : $file_name"
echo "File extension only: $file_extension"
echo "First part of filename only: $file"

Execute the shell script in Linux, run:
chmod +x test.sh
./test.sh
bash test.sh

Conclusion

You learned how to get filename and extension from given path. See “How To Use Bash Parameter Substitution Like A Pro” for more info.


🐧 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
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig 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. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @ webmaster@cyberciti.biz