Hide

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again.

Delete that bloated snippets file you've been using and share your personal repository with the world. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.


If you have a new feature suggestion or find a bug, please get in touch via http://commandlinefu.uservoice.com/

Get involved!

You can sign-in using OpenID credentials, or register a traditional username and password.

First-time OpenID users will be automatically assigned a username which can be changed after signing in.

Hide

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for:

Hide

News

2011-03-12 - Confoo 2011 presentation
Slides are available from the commandlinefu presentation at Confoo 2011: http://presentations.codeinthehole.com/confoo2011/
2011-01-04 - Moderation now required for new commands
To try and put and end to the spamming, new commands require moderation before they will appear on the site.
2010-12-27 - Apologies for not banning the trolls sooner
Have been away from the interwebs over Christmas. Will be more vigilant henceforth.
2010-09-24 - OAuth and pagination problems fixed
Apologies for the delay in getting Twitter's OAuth supported. Annoying pagination gremlin also fixed.
Hide

Tags

Hide

Functions

Batch Convert SVG to PNG

Terminal - Batch Convert SVG to PNG
for i in *; do inkscape $i --export-png=`echo $i | sed -e 's/svg$/png/'`; done
2014-03-24 12:15:58
User: o0110o
Functions: sed
-1
Batch Convert SVG to PNG

Alternatives

There are 5 alternatives - vote for the best!

Terminal - Alternatives
find . -name \*.svg -print0 | xargs -0 -n1 -P4 -I{} bash -c 'X={}; convert "$X" "${X%.svg}.png"'
2014-04-11 14:30:30
User: flatcap
Functions: bash find xargs
2

Convert some SVG files into PNG using ImageMagick's convert command.

Run the conversions in parallel to save time.

This is safer than robinro's forkbomb approach :-)

xargs runs four processes at a time -P4

for i in *.svg; do convert "$i" "${i%.svg}.png"; done
2014-03-24 14:02:02
User: flatcap
1

Convert some SVG files into PNG using ImageMagick's convert command.

svg2png(){ png="${1%.*}.png"; inkscape --export-png="$png" --without-gui "$1" && pngcrush -brute -rem alla -rem text "$png" "$png.new" && mv "$png.new" "$png";}
2013-05-08 15:21:52
Functions: mv
0

Convert an SVG to PNG and then crush the filesize brutally with pngcrush. Good for icons and website junk that you want to keep small, expecially before base64 encoding.

Uses inkscape, not imagemagick, as IM doesn't always handle gradients well. This way also seems to sometime save some file size (eg. 619 with Inkscape compared to 695 with IM).

IM can do general images:

img2png(){ png="${1%.*}.png"; convert -background none "$1" "$png" && pngcrush -brute -rem alla -rem text "$png" "$png.new" && mv "$png.new" "$png"; }
for i in *.svg; do convert "$i" "${i%.svg}.png" & done
2014-03-26 15:03:54
User: robinro
0

convert all *.svg to png _in parallel_

this speeds up the conversion for many files, but might also not be feasible for a large number of files

parallel convert {} {.}.png ::: *.svg
2014-04-12 06:39:02
0

Use GNU Parallel: short, easy to read, and will run one job per core.

Know a better way?

If you can do better, submit your command here.

What others think

A few things about your command.

It's always a good idea to quote your variables: "$i"

Your script will work even if the filenames have whitespace in them.

When calling sub-commands, $(...) is better than `...`

The $(...) form can be nested, which is useful and also you don't have to escape quote marks.

Next, you can use bash to create the output filename (no need for sed), e.g.

echo "${i%.png}.svg"

Remove ".png" from the end of the variable i (and tack .svg on).

echo "${i/.svg/.png}"

will also work but it would get confused on file.svg.txt

So, a tidier version of your command would look like:

for i in *; do inkscape "$i" --export-png="${i%.svg}.png}"; done
Comment by flatcap 39 weeks ago

Sorry, I cut'n'pasted incorrectly. That command should have been:

for i in *; do inkscape "$i" --export-png="${i%.svg}.png"; done
Comment by flatcap 39 weeks ago

Your point of view

You must be signed in to comment.

Related sites and podcasts