2

I want to rename a number of files. I think I can use xargs to do it.

find ./ -name "upload.log-*" 
./upload.log-20180622.gz-20180624.gz-20180626.gz
./upload.log-20180624.gz-20180626.gz
./upload.log-20180620.gz-20180622.gz-20180624.gz-20180626.gz
./upload.log-20180621.gz-20180623.gz-20180625.gz-20180627.gz

find ./ -name "upload.log-*" -print | cut -d"-" -f-2
./upload.log-20180622.gz
./upload.log-20180624.gz
./upload.log-20180620.gz
./upload.log-20180621.gz

I am trying the following but it does not seem to be working

find ./ -name "upload.log-*" | xargs -I '{}' sh -c "echo $1 $(echo $1 | cut -d"-" -f-2)" "{}"

1 Answer 1

6

This does what I need it to do

find ./ -name "cleanup.log-*" | xargs -I '{}' sh -c 'mv "$1" $(echo {} | cut -d"-" -f-2)' - {}

The tricky bit was converting the xargs replacement -I {} string into arguments for the xargs command sh -c ....
I did this by putting a no option argument -, at the end of the command before the replacement string {}.

Thus my command to xargs is the following.
sh -c 'mv "$1" $(echo {} | cut -d"-" -f-2)' - {}

I used this to test what I wanted befor running the move command

find ./ -name "upload.log-*" | xargs -I '{}' sh -c 'echo "$1" $(echo {} | cut -d"-" -f-2)' - {}

I found this site helpful in understanding how to solve this problem

https://explainshell.com

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.