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

find ./ -name "upload.log-*" 

find ./ -name "upload.log-*" -print | cut -d"-" -f-2

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


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


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.