LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-18-2013, 12:03 AM   #1
manjeshjk
LQ Newbie
 
Registered: Dec 2008
Location: Bangalore
Posts: 6

Rep: Reputation: 0
How do rename a file with multiple extensions


I have several files in a particular folder with multiple extensions which I want rename to a single exntension.

Ex: Original file abc.ev.stfm.org has to be renamed as abc.stfm

All the files in the current folder has be to renamed to this particular extension.
 
Old 02-18-2013, 12:54 AM   #2
sevs
Member
 
Registered: Jul 2008
Location: Russia, Saratov
Distribution: debian, knoppix, mandriva, asplinux, altlinux
Posts: 110

Rep: Reputation: 20
If all files in the folder have one multi-extension (i.e. all files match this - *.extensions...) then you could use 'basename'.
If not - try using awk or sed to split file name by '.' and take first result.
Or youcould tryusing internal field separator (IFS) in bash or 'tr'
And where from should 'stfm' extension get in the resulting file name? Is it taken from original file name?

Last edited by sevs; 02-18-2013 at 12:58 AM.
 
Old 02-18-2013, 12:54 AM   #3
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Rep: Reputation: 24
Hi manjeshjk,
Quote:
rename .ev.stfm.org .stfm *.ev.stfm.org
The above command rename all files with .ev.stfm.org extension to .stfm extension files.
 
2 members found this post helpful.
Old 02-18-2013, 01:05 AM   #4
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Quote:
Originally Posted by manjeshjk View Post
I have several files in a particular folder with multiple extensions which I want rename to a single exntension.

Ex: Original file abc.ev.stfm.org has to be renamed as abc.stfm

All the files in the current folder has be to renamed to this particular extension.
Not 100% sure I understand exactly what you want to do, but the following should do what you've asked.
Code:
for f in *.* ; do
  echo mv "$f" "${f%%.*}.stfm" 
done
Note that command actually being run is "echo" so this should just output the command to screen and not actually run it. This way you can check that it will really do what you want.

Evo2.
 
Old 02-18-2013, 01:08 AM   #5
RaviTezu
Member
 
Registered: Nov 2012
Location: India
Distribution: Fedora, CentOs, RHEL
Posts: 164

Rep: Reputation: 24
Quote:
rename .ev.stfm.org .stfm *.ev.stfm.org
You can replace ".ev.stfm.org" with the other extension to rename them to .stfm extentions.

i.e rename .extension_you_have .stfm *.extension_you_have will do the required task.
 
1 members found this post helpful.
Old 02-18-2013, 02:01 AM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
Originally Posted by evo2 View Post
Not 100% sure I understand exactly what you want to do, but the following should do what you've asked.
Code:
for f in *.* ; do
  echo mv "$f" "${f%%.*}.stfm" 
done
Note that command actually being run is "echo" so this should just output the command to screen and not actually run it. This way you can check that it will really do what you want.

Evo2.
@Evo2:
It's not working, but producing same file names & extensions again. Can you check it again?

@manjeshjk::
Can you try this:
Code:
for f in $(ls | gawk -F"." '{print $1}'); do mv $f.* $f.stfm; done
 
Old 02-18-2013, 04:13 PM   #7
evo2
LQ Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and CentOS
Posts: 6,724

Rep: Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705Reputation: 1705
Hi,

Quote:
Originally Posted by shivaa View Post
@Evo2:
It's not working, but producing same file names & extensions again. Can you check it again?
If it is producing the correct command, you can remove the "echo" from the front of the command to actually run it.

Edit: Eh? You're not the OP: how are you testing this?

Evo2.

Last edited by evo2; 02-18-2013 at 04:17 PM.
 
  


Reply



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
rename multiple file by moving part of the name to end of extension cashinke Linux - Newbie 1 08-26-2009 10:55 PM
Multiple File Rename maker10 Linux - Newbie 7 11-06-2007 09:32 AM
rename file extensions danimalz Linux - General 4 05-15-2006 02:30 PM
Multiple File Rename windisch Linux - General 11 11-10-2005 12:19 PM
Multiple file rename jrdioko Linux - Newbie 10 11-07-2004 05:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:49 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
Open Source Consulting | Domain Registration