LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Xargs -i problems. (https://www.linuxquestions.org/questions/linux-software-2/xargs-i-problems-593883/)

Glaurung 10-23-2007 06:12 AM

Xargs -i problems.
 
Howdy!

I had a few files named nameNUMBER (like invoice0102 and so on) which I wanted to rename with a new name but keep the number. I first tried and xargs command that I didn't get to work so I fixed it "by hand" to solve my problem but then went back to the xargs idea to see if I could find out how it works. I fail to get it to work with xargs.

My try was:

find . -type f | xargs -iinvoice## mv invoice## paid##

The shell tries to do (verified with an echo infront of the mv):
mv invoice0102 paid##
mv invoice0103 paid##
... and so on.

So it works like intended apart from the fact that it does not replace the second ##. How can I get xargs to replace both ##?

Thanks a lot for any help!

Regards.

Anders, Gothenburg
Sweden.

KenJackson 10-24-2007 03:43 PM

I think I would do it this way:

for f in invoice*; do mv $f $(echo $f|sed s/invoice/paid/); done

matthewg42 10-25-2007 03:23 AM

better than calling sed for every line of input, which is very slow when there are a lot of files, you can use bash's internal string substitution. It is also good idea to quote the parameters to mv:
Code:

for f in invoice*; do mv "$f" "${f/invoice/paid}"; done


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