Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-09-2010, 12:59 PM
|
#1
|
|
Senior Member
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546
Rep:
|
bash: best way to get filename and extension using bash
I would like to get the filename (without extension) and the extension separately.
The best solution I found so far is:
Let FILE="thefilenameofsomefilesfor_instance.txt"
Code:
NAME=`echo "$FILE" | cut -d'.' -f1`
EXTENSION=`echo "$FILE" | cut -d'.' -f2`
I think it would be better to count the len and remove 3 chars to right to get the extension, but it can be macintosh filenames with have 4 chars for extensions, and this can be hazardous
Last edited by frenchn00b; 01-09-2010 at 01:01 PM.
|
|
|
|
01-09-2010, 01:04 PM
|
#2
|
|
Guru
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594
|
There are likely a stack of ways to do the same; is there anything in particular you don't like about the way you have used above? You could use awk or sed too, but the way you used is probably no better or worse, unless you're considering the difference in nanoseconds that each particular method uses (time to do the operation), in which case cut is probably faster than awk but similar to sed.
Faster yet again, might be ${bash/substitution/substitution} if you are always using bash shell. Check the man page for details on this.
Last edited by GrapefruiTgirl; 01-09-2010 at 01:07 PM.
|
|
|
|
01-09-2010, 01:17 PM
|
#3
|
|
Member
Registered: Jul 2009
Location: Reston, VA
Distribution: Slackware, Ubuntu, RHEL
Posts: 183
Rep:
|
Check out the basename command. Once you have the basename you can sed out the extension.
|
|
|
|
01-09-2010, 01:24 PM
|
#4
|
|
Member
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684
Rep:
|
Check out the handy features at The Advanced BASH Scripting Guide.
Code:
filename="something.extension"
basename=${filename%.*}
extension=${filename##*.}
Regards,
Alunduil
|
|
|
|
01-09-2010, 01:26 PM
|
#5
|
|
Member
Registered: Jul 2009
Location: Reston, VA
Distribution: Slackware, Ubuntu, RHEL
Posts: 183
Rep:
|
Quote:
Originally Posted by alunduil
Code:
filename="something.extension"
basename=${filename%.*}
extension=${filename##*.}
Alunduil
|
huge fan. im getting that one tattooed on my arm.
|
|
|
|
01-09-2010, 01:47 PM
|
#6
|
|
Member
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684
Rep:
|
I have plenty more obscure BASH scripts if you need more tattoos. Those aren't even the fun ones ... lol. Hope that helped out the OP.
Regards,
Alunduil
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 08:52 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|