• Sports
    • NBA
    • NFL
    • MLB
    • NHL
    • Tennis
    • Premier League
    • La Liga
    • Bundesliga
    • Italian Serie A
    • France Ligue 1
  • Movie
    • Movie
    • Action
    • Adventure
    • Animation
    • Biography
    • Comedy
    • Crime
    • Documentary
    • Drama
    • Family
    • Fantasy
    • History
    • Horror
    • Music
    • Musical
    • Mystery
    • Romance
    • Sci-Fi
    • Sport
    • Thriller
    • War
    • Western
  • Collection
    • All News Topics
    • Art & Design
    • Business
    • Education
    • Entertainment
    • Health
    • Lifestyle
    • News
    • Sci & Tech
    • Sports
    • Travel
    • Cooking
    • Fashion
    • Finance
    • Photography
    • Fun
    • Information Technology
    • Products & Appliances
    • Movie & TV
    • Music
    • Animals & Pets
    • Car & Boat
    • Character
  • Info
    • Submit Content
    • About
    • Terms of Use
    • Taable Note Google Play
    • Taable Note iTunes
    • Facebook
    • Google+
    • Twitter
    • Taable Sports Google Play
    • Taable Sports iTunes
  • My Account
    • Login with Facebook
nixCraft: Linux Tips, Hacks, Tutorials, And Ideas In Blog Format
  • Newsstand
  • Information Technology
  • Other information technology
  • nixCraft: Linux Tips, Hacks, Tutorials, And Ideas In Blog Format
  • Bash get filename from given path on Linux or Unix
nixCraft: Linux Tips, Hacks, Tutorials, And Ideas In Blog Format

Bash get filename from given path on Linux or Unix

Source: http://www.cyberciti.biz/faq/bash-get-filename-from-given-path-on-linux-or-unix/ Visit
nixCraft: Linux Tips, Hacks, Tutorials, And Ideas In Blog Format nixCraft: Linux Tips, Hacks, Tutorials, And Ideas In Blog Format Photo
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/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/lsYou can pass multiple arguments using the -a option as follows:basename -a /bin/date /sbin/useradd /nas04/nfs/nixcraft/data.tar.gzStore 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 .gzOR

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"

Bash get filename from given path

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.shHow to Extract Filename & Extension in Shell Script

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.

Latest Articles
Add / Create a Sudo User on CentOS Linux 8

Add / Create a Sudo User on CentOS Linux 8

nixCraft: Linux Tips, Hacks, Tutorials, And Ideas In Blog Format

Vi / Vim Save And Quit The Editor Command

Vi / Vim Save And Quit The Editor Command

nixCraft: Linux Tips, Hacks, Tutorials, And Ideas In Blog Format

Understanding /etc/passwd File Format

Understanding /etc/passwd File Format

nixCraft: Linux Tips, Hacks, Tutorials, And Ideas In Blog Format

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

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

Seeker
Would You Try Programmable Pasta?: These high-tech flat noodles transform into unique 3D shapes with water.
Men's Humor Photo 2018-06-08 22:59

Men's Humor Photo 2018-06-08 22:59

Men's Humor
Jersey Champs is giving away 1000 FREE Black Panther Jerseys this week in honor of reaching one million Instagram followers!
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 Photo 2018-06-08 22:19

ICC - International Cricket Council
Windies build a commanding lead on day three of the first Test against Sri Lanka. 💪

REPORT ➡️ http://bit.ly/WIvsSL1Report3
Highlighted News
ARCore for Android - CNET

ARCore for Android - CNET

Facebook adds skin tones to family emoji, but it’s complicated

Facebook adds skin tones to family emoji, but it’s complicated

Apple could guide you around your city using augmented reality

Apple could guide you around your city using augmented reality

SpaceX successfully launches and recovers Falcon 9 for CRS-12

SpaceX successfully launches and recovers Falcon 9 for CRS-12

More Information Technology
  • Giveaway of the DayGiveaway of the Day
  • ihower { blogging }ihower { blogging }
  • WufooWufoo
  • Feed: Articles & TutorialsFeed: Articles & Tutorials
  • Taable NoteTaable Note
  • Taable AppsTaable Apps
  • Taable FbTaable Fb
  • PinterestPinterest
  • TaableTaable
  • Android and MeAndroid and Me
Taable Note
Taable Sports
Movie
  • Action
  • Adventure
  • Animation
  • Biography
  • Comedy
  • Crime
  • Documentary
  • Drama
  • Family
  • Fantasy
  • History
  • Horror
  • Music
  • Musical
  • Mystery
  • Romance
  • Sci-Fi
  • Sport
  • Thriller
  • War
  • Western
Collection
  • Art & Design
  • Business
  • Education
  • Entertainment
  • Health
  • Lifestyle
  • News
  • Sci & Tech
  • Sports
  • Travel
  • Cooking
  • Fashion
  • Finance
  • Photography
  • Fun
  • Information Technology
  • Products & Appliances
  • Movie & TV
  • Music
  • Animals & Pets
  • Car & Boat
  • Character
Sports
  • NBA
  • NFL
  • MLB
  • NHL
  • Tennis
  • Premier League
  • La Liga
  • Bundesliga
  • Italian Serie A
  • France Ligue 1
Info
  • Submit Content
  • About
  • Terms of Use

© Copyright 2019 Taable Note. All rights reserved.