ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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
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?
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.
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
'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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.