LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Changing extension of multiple files (https://www.linuxquestions.org/questions/programming-9/changing-extension-of-multiple-files-388053/)

harisund 11-30-2005 06:16 PM

Changing extension of multiple files
 
Hello

Here is my task. I have a list of files cs4103??.src Where the question marks stand for 2 digits.

I need all the files (cs410301.src through cs410344.src) converted into corresponding .c files. Meaning I want to change the extensions of all the files to .c keeping the name constant.

In Windows (command prompt) I used to
ren *.src *.c

However, in BASH I tried

cp *.src c_source/*.c #c_source is a folder
mv *.src *.c

Is there any other tool that I might not be aware of for this?

Thanks

Hari

Mad Scientist 11-30-2005 06:31 PM

It they are all in one directory, you could try

Code:

rename .src .c *

Dommy 11-30-2005 11:10 PM

Well thats one command I never knew about, bugger. When I needed to rename multiple files I used to create a shell script using vi, this would have been so much easier :)

harisund 11-30-2005 11:24 PM

That is what I too though, is it rename, or did you mean mv?

And also, you wrote a shell script? Could you please guide me as to what you would have written if you only had to change the extension and not the file name?

Thanks !

Wim Sturkenboom 12-01-2005 12:27 AM

It's rename (see man rename). And I also did not know that one :(

chenglim 12-01-2005 01:17 AM

Assuming in same directory. This remind me my Uni time

for f in *.src; do
base=`basename $f .src`
mv $f $base.c
done

vls 12-01-2005 01:25 AM

Quote:

Originally posted by harisund
That is what I too though, is it rename, or did you mean mv?

And also, you wrote a shell script? Could you please guide me as to what you would have written if you only had to change the extension and not the file name?

Thanks !

one way in bash:

Code:

for file in *.src
do
mv ${file} ${file%.src}.c
done

and another:
Code:

for file in *.src
do
mv ${file} ${file/.src/.c}
done

Some notes on previous comments:
mv and cp do not work the way the dos equivalents work.

mv *.src *.c is saying mv all files with .src extension and all files with .c extension so I'm guessing that barfed on you when you tried it.

You have to process each file individually to do the extension substitution thing which is what the two loops above do.

You can run those straight on the command line or put it in a file as a shell script.

The rename command is more than likely the binary equivalent of the above shell code.

mikejac69 12-01-2005 01:34 AM

this is the script I would have used, rather simple as I am just starting messing around with scripts, anyone has any improvments, feel free to send them!

#!/bin/bash
#changing file extensions
for i in *.src
do
x=`echo "$i"|sed -e 's/src/c/'`
cp "$i" /home/mike/tester/"$x"
done
#the final destinations should be changed ($i.c) so that you do not end up with both src and c files in the same directory
done

This will copy your source files to the destination (in this case /home/mike/tester) and rename them as *.c

mikejac69 12-01-2005 01:36 AM

#!/bin/bash
#changing file extensions
for i in *.src
do
x=`echo "$i"|sed -e 's/.src/.c/'`
cp "$i" /home/mike/tester/"$x"
done

oops, missed the dots!!

I also see some others have already posted their scripts whilst I was writing mine :D

chenglim 12-01-2005 01:50 AM

"mv" is a move (rename) on Unix/Linux/BSD/...

"ren" is a rename for DOS/Windows...

'basename' is a util to return filename without extension. For example
"1.c" return "1"

# line 1: get every file from current directoy with .src extension, start loop with do
for f in *.src; do
# line 2: strip the basename of .src
base=`basename $f .src`
# line 3: copy file from source to c_source folder with extension .c
cp $f c_source/$base.c
# line 4: end loop with done
done

for f in *.src; do
base=`basename $f .src`
cp $f c_source/$base.c
done

mv *.src *.c may not work in all shell. The wild card (*) is not valid. you need use a "for' in shell script.

vlad-eliseev 12-01-2005 02:15 AM

Try this chain:

ls cs4103*.src | sed 's/src//g' | xargs -i mv {}src {}c

Regards,
Vladimir

gheesh 12-01-2005 05:04 AM

Under Debian, which uses the Perl rename command (other distros might use it as well) you can "simply" do:

rename 's/src$/c/' *

gaurav_gupta082 12-01-2005 10:41 PM

Answer of changing extension of multiple file
 
hi this is your solution, please run this shell script to convert files from .src extension to .c extension.

for file in *.src ; do
leftname=`basename $file src`
mv $file ${leftname}c
done

ChristophThoel 12-14-2005 05:15 AM

Changing extension of multiple files
 
hello,
you may try the following bash script I wrote (the tricks used
therein can be found in the books "Linux in a nutshell (OReilly...)"
and Michael Kofler's "Linux - Installation, Konfiguration, Anwendung",
the latter is written in German).

My script comes with no warranty whatsoever ... ...
try it first on some 3 or 4 files, before changing 44 filenames.

#!/bin/bash
# Filename ren_ext ( cht 13-Dec-2005 )
# Purpose : Rename all indicated files in current directory,
# replacing the file extension .old with .new
#
# Usage:
# ren_ext old new file [ file ... ]
# where old and new are any names used as filename extensions
# Effect:
# Each <something>.old in the list of files is renamed to <something>.new
# Caution:
# Any file <something> (without extension .old) in the list of files
# is renamed to <something>.new
#----------------------------------------------------------------------------
# Example: In the current directory there are files
# fic.test, mic.test, ficfic ( and no other *.test ).
# running
# ren_ext test tzt *.test ficfic
# will rename those 3 files to
# fic.tzt, mic.tzt, ficfic.tzt

if [ $# -lt 3 ] ; then
echo "Usage:"
echo " $0 old new file [ file ... ]"
exit 1
fi

old=$1; shift
new=$1; shift

while [ $# -gt 0 ] ; do
item=$1; shift
# echo "item: $item"
mv $item ${item%.$old}.$new
done
exit 0
# That's all folks

cht (Christoph Thöl)

harisund 12-14-2005 05:23 AM

Wow ! Thanks a real lot folks
 
Never did I expect so many replies.. and so many varied methods from sed commands to bash tricks.

Thanks a lot again..

Hari


All times are GMT -5. The time now is 05:10 PM.