I would expect under bash for the following:
ls *.py | xargs -I{} echo $(echo {} | sed 's/\.py/_2\.py/g')
to list all .py files contained in the directory but with _2 annexed after the file name and before the .py extension.
Yet this is not what happens. It simply lists the .py files in the directory without any change.
In short:
$ ls
A.py B.py
$ ls *.py | xargs -I{} echo $(echo {} | sed 's/\.py/_2\.py/g')
A.py
B.py
while the output I expected would've been:
A_2.py
B_2.py
What happens here and how to get the expected output?
ls *.py | xargs -I{} echo {} | sed 's/\.py/_2\.py/g'
? – Philippos Aug 30 '19 at 08:06ls *.py | sed 's/\.py/_2\.py/g'
works... the reason is that this is testing, I want to replace the echo after the-I{}
with a command likecp
,mv
, ... – Amaterasu Aug 30 '19 at 08:09ls
and usefind ... -exec ...
instead. See also https://unix.stackexchange.com/q/128985/364705 – markgraf Aug 30 '19 at 08:11find . -type f -name '*.py' -exec sh -c 'a={}; mv "$a" "${a%.py}_2.py"' \;
– markgraf Aug 30 '19 at 08:38find -name "*.py" -exec cp -n {} {}_2 \; -exec rename .py_2 _2.py {} \;
otoh is less baroque but it can't be tested ... and is it really safe? – Amaterasu Aug 30 '19 at 09:00