LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-30-2005, 06:16 PM   #1
harisund
Member
 
Registered: Sep 2005
Location: Baton Rouge
Distribution: Ubuntu 5.10
Posts: 74

Rep: Reputation: 15
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
 
Old 11-30-2005, 06:31 PM   #2
Mad Scientist
Member
 
Registered: May 2003
Posts: 167

Rep: Reputation: 30
It they are all in one directory, you could try

Code:
rename .src .c *
 
Old 11-30-2005, 11:10 PM   #3
Dommy
Member
 
Registered: Jul 2004
Location: Canberra
Distribution: Mint 7
Posts: 204

Rep: Reputation: 30
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
 
Old 11-30-2005, 11:24 PM   #4
harisund
Member
 
Registered: Sep 2005
Location: Baton Rouge
Distribution: Ubuntu 5.10
Posts: 74

Original Poster
Rep: Reputation: 15
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 !
 
Old 12-01-2005, 12:27 AM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
It's rename (see man rename). And I also did not know that one
 
Old 12-01-2005, 01:17 AM   #6
chenglim
LQ Newbie
 
Registered: Jan 2005
Posts: 18

Rep: Reputation: 0
Assuming in same directory. This remind me my Uni time

for f in *.src; do
base=`basename $f .src`
mv $f $base.c
done
 
Old 12-01-2005, 01:25 AM   #7
vls
Member
 
Registered: Jan 2005
Location: The grassy knoll
Distribution: Slackware,Debian
Posts: 192

Rep: Reputation: 31
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.
 
Old 12-01-2005, 01:34 AM   #8
mikejac69
Member
 
Registered: May 2004
Location: Lyon, France
Posts: 53

Rep: Reputation: 15
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
 
Old 12-01-2005, 01:36 AM   #9
mikejac69
Member
 
Registered: May 2004
Location: Lyon, France
Posts: 53

Rep: Reputation: 15
#!/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
 
Old 12-01-2005, 01:50 AM   #10
chenglim
LQ Newbie
 
Registered: Jan 2005
Posts: 18

Rep: Reputation: 0
"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.
 
Old 12-01-2005, 02:15 AM   #11
vlad-eliseev
LQ Newbie
 
Registered: Jan 2003
Location: Russia, Moscow
Distribution: ASPLinux, RedHat, Mandrake
Posts: 1

Rep: Reputation: 0
Try this chain:

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

Regards,
Vladimir
 
Old 12-01-2005, 05:04 AM   #12
gheesh
LQ Newbie
 
Registered: Mar 2004
Location: Spain
Distribution: Debian
Posts: 5

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

rename 's/src$/c/' *
 
Old 12-01-2005, 10:41 PM   #13
gaurav_gupta082
LQ Newbie
 
Registered: May 2005
Posts: 4

Rep: Reputation: 0
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
 
Old 12-14-2005, 05:15 AM   #14
ChristophThoel
LQ Newbie
 
Registered: Sep 2003
Posts: 8

Rep: Reputation: 0
Cool 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)
 
Old 12-14-2005, 05:23 AM   #15
harisund
Member
 
Registered: Sep 2005
Location: Baton Rouge
Distribution: Ubuntu 5.10
Posts: 74

Original Poster
Rep: Reputation: 15
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
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
changing ownership and group of multiple files.. utanja Debian 3 02-21-2005 07:10 PM
symlink multiple files to new directory with new extension jmanjohn61 Linux - General 1 01-06-2005 01:55 PM
What to do about these files with .new extension? Slovak Slackware 1 11-23-2004 05:50 PM
How program to read files with extension .dat y .cfg files COMTRADE in fedora 1? ivonne Linux - Software 0 11-22-2004 12:42 PM
changing extension of multiple files shaggystyle Linux - General 5 01-29-2004 01:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:53 PM.

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