LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 10-15-2005, 11:17 PM   #1
Berticus
Member
 
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:
Code:
#!/bin/bash

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

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

For instance, try this on the command line:
Code:
$ 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.
 
  


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 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 06:52 AM
making a bash script student04 Linux - Software 5 01-11-2005 04:41 AM
making a script that will move a file or files in a trash folder Paxmaster Programming 5 12-12-2004 07:00 PM
how to creata file through bash script? varala_kanth Linux - Software 2 04-29-2004 03:52 PM
file editing in a bash script Harpune Programming 4 11-23-2002 12:35 AM


All times are GMT -5. The time now is 03:58 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration