Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 02-14-2005, 10:58 AM   #1
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
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, 11:08 AM   #2
Senior Member
Registered: Aug 2003
Location: Glasgow
Distribution: Fedora / Solaris
Posts: 3,109

Rep: Reputation: 96

basename myfile.tar.gz .tar.gz

Old 02-14-2005, 11:15 AM   #3
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, 02:06 AM   #4
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

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

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

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.


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

All times are GMT -5. The time now is 12:48 PM.

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