0

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

0

3 Answers 3

1

Depending on your distribution, you can use rename:

rename 's/(.*)(\.xml)/$1_R$2/' *.xml
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

1 Comment

A little description of this solution wouldn't hurt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.