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?
My solution is similar to many of the xarg solutions, and particularly similar to Schleis'.
The difference here is a full regex manipulation with match references, and sed commands that properly ignore files that don't match so you don't need to prefilter your listing.
This is also safe for files with spaces and shell meta.
Change
\2
in the replacement to any desired extension.Explanation
The
-n
arg tell's sed not to print anything by default, theT
command says skip to the end of the script if the previouss
command didn't do a replacement, thep
command prints the pattern space (only hit if thes
command matches).The
&
in the replacement is a reference to the contents of the original filename match.If we replace
mv
in the command withbash -c 'echo "run($#) $@"' bash
then we can see the number of timesmv
would be called, and with parameter count and value:Coming late to the party, but here's how you can rename files with xargs. Say you have a bunch of files named fileN.svg.png and you want to name them fileN.png where N could be a series of integers:
ls *.svg.png | xargs basename -s .svg.png | xargs -I {} mv {}.svg.png {}.png
The first xargs uses basename to strip off both .svg and .png to get a just
filenameN
. The second xargs receives that bare name and uses replacement to rename the file.