LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 06-26-2005, 12:13 AM   #1
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,090

Rep: Reputation: 41
bash help, renaming file extensions


I have a directory with about 2,000 files that have various differn't file extensions. I need to get these all these files to have the same extension of .bin or .txt something other then what is there now

Can anyone help a scripting newb, or can this be done with ls and sed?

Thanks
Trey
 
Old 06-26-2005, 12:27 AM   #2
mjrich
Senior Member
 
Registered: Dec 2001
Location: New Zealand
Distribution: Debian
Posts: 1,046

Rep: Reputation: 45
Just use a Bash script
Code:
#!/bin/bash
for f in *.*; do
     mv -v $f `echo $f | cut -f1 -d. -`.txt
     done
Cheers,

mj
 
Old 06-26-2005, 01:12 AM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 368Reputation: 368Reputation: 368Reputation: 368
Just a warning: mjrich's script will work, but it assumes there's only one '.' in the filename. For example, if there was a tarball named "my_backup.tar.bz2", the script would rename it to "my_backup.txt".

Another alternative would be to just slap the extension on the end:
Code:
#!/bin/bash

for filename in *
do
  echo "mv -v ${filename} ${filename}.txt"
  # mv -v ${filename} ${filename}.txt
done
Run the script and scan the output to make sure it looks like it's doing what it should. When you're ready, remove the comment marker '#' from the mv command, and run again. It's not extremely elegant, but it preserves the original filename so you can go back later if need be.
 
Old 06-26-2005, 09:41 AM   #4
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,090

Original Poster
Rep: Reputation: 41
Thumbs up

thanks to both of you, both scripts worked as desired
 
Old 06-27-2005, 05:39 PM   #5
mjrich
Senior Member
 
Registered: Dec 2001
Location: New Zealand
Distribution: Debian
Posts: 1,046

Rep: Reputation: 45
Re: Just a warning: ....assumes there's only one '.' in the filename.

Cheers -- I should have realised this, as I've fallen into the same trap before

mj
 
Old 06-28-2005, 11:04 PM   #6
trey85stang
Senior Member
 
Registered: Sep 2003
Posts: 1,090

Original Poster
Rep: Reputation: 41
Quote:
Originally posted by mjrich
Re: Just a warning: ....assumes there's only one '.' in the filename.

Cheers -- I should have realised this, as I've fallen into the same trap before

mj
oddly enough.. there were a few files with more then two .'s but those files were skipped. Only about 20 of them anyways so it was no big deal
 
Old 06-29-2005, 04:55 PM   #7
mjrich
Senior Member
 
Registered: Dec 2001
Location: New Zealand
Distribution: Debian
Posts: 1,046

Rep: Reputation: 45
Interesting -- must be a different version of Bash or cut perhaps... my ones plow right on through and rename all *.*, including the double dotted files.

Cheers,

mj
 
Old 06-30-2005, 02:00 AM   #8
mohit dhawan
Member
 
Registered: Mar 2005
Posts: 92

Rep: Reputation: 15
echo "mv -v ${filename} ${filename}.txt"

okay confused
just learning shell scrpiting

how does it change the extension
because the move cmd is in quotes and echo would just print that on screen


 
Old 07-21-2005, 04:51 PM   #9
lampeasouder
LQ Newbie
 
Registered: Jul 2005
Distribution: Darwin Ubuntu OSX Debian
Posts: 1

Rep: Reputation: 0
With a little regexp and sed, this could be OK:

for file in $(find /the path/myrep/*) # This work with files in sub directories (absolute path)
do
# Moove only regular files and change the last .something with .txt
[ -f $file ] && mv $file $(echo $file|sed 's/.[a-zAZ-09]*$/.txt/')
done


I think that's ok.

Last edited by lampeasouder; 07-21-2005 at 04:54 PM.
 
  


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
dirname basename; how to capture extensions from bash? carl0ski Linux - General 2 07-22-2005 07:26 AM
Batch Renaming in bash xushi Programming 6 07-07-2005 03:24 PM
bash help renaming files kahn Programming 6 06-16-2005 07:15 AM
How do I make a bash sheet for renaming files? Cyberian Linux - Newbie 16 03-16-2005 08:08 AM
Bash script renaming a variable zael Programming 3 09-30-2003 04:37 AM


All times are GMT -5. The time now is 01:47 AM.

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