Bash get filename from given path on Linux or Unix


Bash get filename from given path
The basename command strip directory and suffix from filenames. The syntax is pretty simple:basename /path/to/filebasename /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.shbash 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.
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
.

Would You Try Programmable Pasta? - Seeker 2018-06-09 00:00
Seeker

Men's Humor Photo 2018-06-08 22:59
Men's Humor
Simply tag a Black Panther fan in this post and click the link below to get your free jersey!🔥🔥🔥
https://www.jerseychamps.com/products/black-panther-tchalla-killmonger-basket

ICC - International Cricket Council Photo 2018-06-08 22:19
ICC - International Cricket Council
REPORT ➡️ http://bit.ly/WIvsSL1Report3