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
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]$
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