remove file extension with basename when using xargs

120 views Asked by At

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
2

There are 2 answers

2
anubhava On

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
3
Fravadona On

With GNU sed:

printf '%s\0' *.las | sed -z 's/las$/ply/' | xargs -0 -n 1 -P 10