1

I would like to rename some files and exclude others into a directory. I tried

find . -mindepth 1 -maxdepth 1 ! -name 000-default.conf ! -name default-ssl.conf -print0 |
xargs -0 -I {} sudo mv -- {} $(echo {} | sed 's/local.conf/local.example.com.conf/')

but it wouldn't work.

I think mv command's second argument doesn't expand correctly.

Bruno
  • 19
  • What makes you think that? What happens when you run that command? Please provide the output, and preferably a simplified test case. – Mikel Sep 23 '15 at 02:31

2 Answers2

2

The $(...) part is being evaluated by the shell before xargs is even called. What you might try is something like:

xargs -0 -I {} bash -c 'mv {} $(echo {} | sed "s/local.conf/local.example.com.conf/")'

You can avoid the sed with

xargs -0 -I {} bash -c 'f="{}"; mv "{}" "${f/local.conf/local.example.com.conf}"'

See also the rename command if you have it.

meuh
  • 51,383
0

You could also write it without the xargs, something like

find . -mindepth 1 -maxdepth 1 ! -name 000-default.conf ! -name default-ssl.conf -exec sh -c '
sudo mv -- "$0" "$(echo "$0" | sed "s/local.conf/local.example.com.conf/")"

' {} \;

See How does this find command using "find ... -exec sh -c '...' sh {} +" work? for more details.

Mikel
  • 57,299
  • 15
  • 134
  • 153