Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 10-15-2005, 10:17 PM   #1
Registered: Jul 2005
Distribution: Arch
Posts: 159

Rep: Reputation: 31
making a bash script file

I hope I'm calling it correctly...

Anyway, I'd like to know how to take off the extension of a file. For example, let's say I have something.txt and somethingelse.txt, how do I make a bash script to just look at it and see something and somethingelse? I've got the first part down:

for i in *.txt; do
mv $i $i.yo
This would rename the files into something.txt.yo and somethingelse.txt.yo instead of something.yo and somethingelse.yo.
Old 10-15-2005, 11:14 PM   #2
Senior Member
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
There are a couple different ways, but I usually end up using sed.

For instance, try this on the command line:
$ echo "something.txt" | sed 's/\.txt$/.yo/'
Here's some of an explanation of what's happening:
sed looks at the input text ("something.txt" in this case) and tries to match ".txt" at the end of the input. Side note: the '\\' in front of the period with "txt" is necessary. If sed finds text that matches, it replaces that text with ".yo". For this case, the '\\' is not necessary for the period before "yo". The details of which are long. You should be able to read about why in the sed man page (man sed) or with a Google search about regular expressions.

Anyway, the point is, the output of that command gives you the name of the file with the extension replaced. The command will work with any extention - you just have to replace "txt" for the extension you want to match and "yo" with the extension you want to replace it with.

If you want to work it into your script as a learning exercise, I'll hold off on posting how to integrate it. If you just want to "get it to work" then say so, and I'll show you how I'd work the command in.


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 On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
making select show its menu in a bash script? zidane_tribal Programming 6 05-02-2005 05:52 AM
making a bash script student04 Linux - Software 5 01-11-2005 03:41 AM
making a script that will move a file or files in a trash folder Paxmaster Programming 5 12-12-2004 06:00 PM
how to creata file through bash script? varala_kanth Linux - Software 2 04-29-2004 02:52 PM
file editing in a bash script Harpune Programming 4 11-22-2002 11:35 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:04 AM.

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