LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-14-2005, 09:58 AM   #1
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Rep: Reputation: 32
Question Getting the first part of a filename in a BASH script


Hi all, hopefully someone will be able to help with a little shell scripting problem.

Basically, I need to be able to find out the "first part" of a filename, i.e. the bit before the extension. I'm only looking at this for .tar.gz files (so if, say, there was a file called myfile.tar.gz, I'd want to get the result myfile). The first solution that came to mind was
Code:
cut -d'.' -f1
but this doesn't work if there's a dot anywhere else in the name (like my.file.tar.gz). If anyone's got any ideas, they'll be much appreciated.
 
Old 02-14-2005, 10:08 AM   #2
ilikejam
Senior Member
 
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96
Hi.

Try:
basename myfile.tar.gz .tar.gz

Dave
 
Old 02-14-2005, 10:15 AM   #3
trevelluk
Member
 
Registered: Nov 2003
Location: Bristol, UK
Distribution: Debian Lenny, Gentoo (at work)
Posts: 388

Original Poster
Rep: Reputation: 32
Great, that works perfectly! Thanks.
 
Old 02-15-2005, 01:06 AM   #4
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
If the filename is in a variable you could use variable substitution.

> bn=myfile.tar.gz
> echo ${bn%%.*}
myfile

This could be handy in scripts.
Now, if the filename contains a dot character, then this won't work. If you want to remove only the last extension, use ${variable-name%.*} instead.
 
  


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
Bash script to remove capitalisation and spaces form a filename scuzzman Programming 11 05-18-2008 12:28 PM
Simple bash script help, grabbing part of a string colabus Linux - Newbie 3 04-25-2005 09:42 AM
put date as part of filename box_l Programming 3 07-27-2004 06:17 AM
Bash script to change a filename associated with an inode index number. Ziv Programming 22 06-19-2004 08:41 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM


All times are GMT -5. The time now is 07:43 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration