So, I have the following structure:
.
..
a.png
b.png
c.png
I ran a command to resize them
ls | xargs -I xx convert xx -resize xx.jpg
Now my dir looks like this
.
..
a.png.jpg
a.png
b.png.jpg
b.png
c.png.jpg
c.png
The firs question is, how do i rename the file so that I can just have one extension. Not two. (basically, how do I clean up my original mistake)?
The second question is, in the future, using xargs, how do I change the extension of the file simular to second command?
To my knowledge, that can't be done. The best way to do it would be to use a for-loop with parameter substitution much like the one above:
If you have files in subdirectories that you want converted, then you can pipe
find
to awhile read
loop:NOTE: It's generally considered a bad idea to use the output of
ls
in a shell script. While your example might have worked fine, there are lot's of examples where it doesn't. For instance, if your filenames happened to have newlines in them (which unix allows),ls
probably won't escape those for you. (That actually depends on your implementation, which is another reason not to usels
in scripts; it's behavior varies greatly from one box to the next.) You'll get more consistent results if you either usefind
in a while-read loop or file globbing (e.g.*.png
) in a for loop.I'm late to this party by about 3 years, I just had a similar problem which I figured out myself. I had a list of png files which I converted using inkscape, because ImageMagick's svg support is poor.
I originally converted them by doing:
Which of course led to the same issue like posted above.
I wanted to rename *.svg.png to *.png, this is what I wound up with...
This does three things:
EDIT
I realize now this is the most convoluted way to do this. One can simply use the rename command.
To clean up your error, try the
rename
utility. Check the manpage for details.In your case, you'd do
rename '.png.jpg' '.jpg' ./*
if your current directory is set appropriately.Since you have
convert
available, I'm assuming you havemogrify
too (imagemagick
suite). Whenever I want to do this, I copy the files into a different directory and usemogrify
instead. I usually need this only for resizing, but if you change the image format aswell,mogrify
will handle the filenames (make new files with proper filenames).You would use it as
mogrify -format jpg -resize [size] ./*.png
. I'm not sure what-resize
without geometry arguments is supposed to do. It isn't documented and doesn't work on my machine.As Tim Pote reasoned, I don't think you can make xargs handle filenames and extensions separately.
This can be also be done with
xargs
andsed
to change the file extension.You can print the original filename along with what you want the filename to be. Then have xargs use those two arguments in the move command. For the one-liner, I also added a grep to filter out anything not a *.png file.
My attempt from: https://www.tecmint.com/linux-image-conversion-tools/
Using parallel
find ./xx -name "*.png" -print0 |sed 's/.png$//g'|xargs -0 -I% mv %.png %.jpg