LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How do rename a file with multiple extensions (https://www.linuxquestions.org/questions/linux-newbie-8/how-do-rename-a-file-with-multiple-extensions-4175450604/)

manjeshjk 02-18-2013 12:03 AM

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.

sevs 02-18-2013 12:54 AM

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?

RaviTezu 02-18-2013 12:54 AM

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.

evo2 02-18-2013 01:05 AM

Quote:

Originally Posted by manjeshjk (Post 4894137)
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.

RaviTezu 02-18-2013 01:08 AM

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.

shivaa 02-18-2013 02:01 AM

Quote:

Originally Posted by evo2 (Post 4894166)
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

evo2 02-18-2013 04:13 PM

Hi,

Quote:

Originally Posted by shivaa (Post 4894187)
@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.


All times are GMT -5. The time now is 08:25 PM.