2

I would like to use xargs to run a command on a number of files where the output from the command has a different file extension. For the example below I am using echo as a dummy command.

ls *.las | xargs -I % -n1 -P 10 echo $(basename % .las).ply

this produces the output

ID1.las.ply
ID2.las.ply
ID3.las.ply

This does not strip the .las and only adds the new .ply file extension. What I would like is:

ID1.ply
ID2.ply
ID3.ply
kungphil
  • 1,759
  • 2
  • 18
  • 27
  • 2
    the reason it doesn't work is that `$(basename % .las)` happens before `xargs` is run. It returns `%`. So xargs just sees `... echo %.ply` – jhnc Aug 31 '23 at 10:40

3 Answers3

2

No need to use ls | xargs. Just do it in pure bash itself:

for i in *.las; do
   echo "${i%.las}.ply"
done

If you need to ls | xargs then use:

ls *.las | xargs -P 10 bash -c 'for f; do echo "${f%.las}".ply; done' _

ID1.ply
ID2.ply
ID4.ply
anubhava
  • 761,203
  • 64
  • 569
  • 643
1

With GNU sed:

printf '%s\0' *.las | sed -z 's/las$/ply/' | xargs -0 -n 1 -P 10
Fravadona
  • 13,917
  • 1
  • 23
  • 35
-1

This might help!

ls *.las | sed -e 's/\.las$//'| xargs -I % -n1 -P 10 echo $(basename %).ply

If you want to run it for different file extension the just update sed command as below ,

sed 's/\.[^.]*$//'