using xargs pass arguments to sub shell with pipe
5,413
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
Related videos on Youtube
Author by
nelaaro
Linux admin, tech enthusiast. opensource evangelist.
Updated on September 18, 2022Comments
-
nelaaro over 1 year
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)" "{}"