Reputation: 39
I have 3 text files.I want to modify the file name of those files using for loop as below .
Please find the files which I have
1234.xml
333.xml
cccc.xml
Output:
1234_R.xml
333_R.xml
cccc_R.xml
Upvotes: 0
Views: 161
Reputation: 1178
You can use a for loop to iterate over a list of words (e.g. with the list of file names returned by ls
) with the (bash) syntax:
for name [ [ in [ word ... ] ] ; ] do list ; done
Then use mv source dest
to rename each file.
One nice trick here, is to use basename
, which strips directory and suffix from filenames (e.g. basename 333.xml
will just return 333
).
If you put all this together, the following should work:
for f in `ls *.xml`; do mv $f `basename $f`_R.xml; done
Upvotes: 0
Reputation: 1028
Just basic unix command mv
work both on move and rename
mv 1234.xml 1234_R.xml
If you want do it by a large amount, do like this:
[~/bash/rename]$ touch 1234.xml 333.xml cccc.xml
[~/bash/rename]$ ls
1234.xml 333.xml cccc.xml
[~/bash/rename]$ L=`ls *.xml`
[~/bash/rename]$ for x in $L; do mv $x ${x%%.*}_R.xml; done
[~/bash/rename]$ ls
1234_R.xml 333_R.xml cccc_R.xml
[~/bash/rename]$
Upvotes: 1
Reputation: 29211
Depending on your distribution, you can use rename
:
rename 's/(.*)(\.xml)/$1_R$2/' *.xml
Upvotes: 1