LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 01-09-2010, 12:59 PM   #1
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,561

Rep: Reputation: 57
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.
 
Old 01-09-2010, 01:04 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
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.
 
Old 01-09-2010, 01:17 PM   #3
GooseYArd
Member
 
Registered: Jul 2009
Location: Reston, VA
Distribution: Slackware, Ubuntu, RHEL
Posts: 183

Rep: Reputation: 46
Check out the basename command. Once you have the basename you can sed out the extension.
 
Old 01-09-2010, 01:24 PM   #4
alunduil
Member
 
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

Rep: Reputation: 62
Check out the handy features at The Advanced BASH Scripting Guide.

Code:
filename="something.extension"
basename=${filename%.*}
extension=${filename##*.}
Regards,

Alunduil
 
Old 01-09-2010, 01:26 PM   #5
GooseYArd
Member
 
Registered: Jul 2009
Location: Reston, VA
Distribution: Slackware, Ubuntu, RHEL
Posts: 183

Rep: Reputation: 46
Quote:
Originally Posted by alunduil View Post
Code:
filename="something.extension"
basename=${filename%.*}
extension=${filename##*.}
Alunduil
huge fan. im getting that one tattooed on my arm.
 
Old 01-09-2010, 01:47 PM   #6
alunduil
Member
 
Registered: Feb 2005
Location: San Antonio, TX
Distribution: Gentoo
Posts: 684

Rep: Reputation: 62
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
filename escaping in bash dash9 Linux - Software 1 06-04-2009 10:36 PM
changing filename using Bash Script tmbigrigg Programming 10 11-10-2007 07:02 AM
bash, cp and filename (with spaces) woes dfidler Programming 9 04-10-2007 11:12 PM
Getting the first part of a filename in a BASH script trevelluk Programming 3 02-15-2005 01:06 AM
using bash in cron to get the date in filename rstoutmna Programming 1 12-29-2004 02:39 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration