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

Commands tagged pdf from sorted by
Terminal - Commands tagged pdf - 55 results
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dFirstPage=14 -dLastPage=17 -sOutputFile=OUTPUT.pdf ORIGINAL.pdf
mergepdf() { gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=merged.pdf "$@" }
2011-07-28 11:12:15
Functions: gs
1

This is an expansion on a previous entry, which I've wrapped in a function and placed in my profile. The "$@" is a positional parameter, much like "$*", but the parameters are passed on intact, without interpretation or expansion; so you can simply call the function like this:

mergepdf *

This will output a merged PDF of all PDFs in the current directory. Alternatively, you can simply list them like so:

mergepdf 00.pdf 01.pdf 02.pdf ...

N.B. Passing a wildcard will merge all PDFs in the current directory in name order, e.g. 00.pdf 01.pdf aa.pdf ab.pdf

pdftk input1.pdf input2.pdf cat output output.pdf
gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=output.pdf input1.pdf input2.pdf ...
pdfimages -j foo.pdf bar
2011-07-07 17:18:36
User: lucasrangit
Tags: pdf jpeg
3

This will extract all DCT format images from foo.pdf and save them in JPEG format (option -j) to bar-000.jpg, bar-001.jpg, bar-002.jpg, etc.

Inspired by http://stefaanlippens.net/extract-images-from-pdf-documents

wget -r -A .pdf -l 5 -nH --no-parent http://example.com
2011-06-09 17:17:03
User: houghi
Functions: wget
Tags: wget pdf
7

See man wget if you want linked files and not only those hosted on the website.

curl -s http://example.com | grep -o -P "<a.*href.*>" | grep -o "http.*.pdf" | xargs -d"\n" -n1 wget -c
2011-06-09 14:42:46
User: b_t
Functions: grep wget xargs
0

This example command fetches 'example.com' webpage and then fetches+saves all PDF files listed (linked to) on that webpage.

[*Note: of course there are no PDFs on example.com. This is just an example]

gswin32c -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sFONTPATH=%windir%/fonts;xfonts;. -sPDFPassword= -dPDFSETTINGS=/prepress -dPassThroughJPEGImages=true -sOutputFile=OUTPUT.pdf INPUT.pdf
convert -compress jpeg *.jpg mydoc.pdf
pdftk A=odd.pdf B=even.pdf shuffle A1-end Bend-1S output duplex.pdf
2011-02-25 15:00:09
User: till
5

Joins two pdf documents coming from a simplex document feed scanner. Needs pdftk >1.44 w/ shuffle.

pdftk in.pdf cat 1S output out.pdf
convert -density 300x300 input.pdf output.png
2011-01-28 00:36:31
2

If you skip this part:

-density 300x300

you'll get a very lo-res image.

pdfnup --nup 2x1 --frame true --landscape --outfile output.pdf input.pdf
2010-12-21 14:20:06
User: TetsuyO
Functions: true
3

This is an example of the usage of pdfnup (you can find it in the 'pdfjam' package). With this command you can save ink/toner and paper (and thus trees!) when you print a pdf.

This tools are very configurable, and you can make also 2x2, 3x2, 2x3 layouts, and more (the limit is your fantasy and the resolution of the printer :-)

You must have installed pdfjam, pdflatex, and the LaTeX pdfpages package in your box.

man -t manpage | ps2pdf - filename.pdf
2010-12-19 22:40:18
User: TetsuyO
Functions: man
47

Quick and dirty version. I made a version that checks if a manpage exists (but it's not a oneliner). You must have ps2pdf and of course Ghostscript installed in your box.

Enhancements appreciated :-)

pdftohtml -i -stdout FILE.pdf | w3m -T text/html
2010-11-03 12:01:01
User: Haegin
Tags: pdf w3m
4

Turns a PDF into HTML (without images) and prints it to the standard out which is picked up and interpreted by w3m.

markdown doc.md | htmldoc --cont --headfootsize 8.0 --linkcolor blue --linkstyle plain --format pdf14 - > doc.pdf
2010-10-31 13:17:28
User: tijs
1

This is the one-line version of this htmldoc + markdown combo to convert markdown formatted text to PDF files to distribute to your non-savvy project managers.

http://scottnesbitt.net/ubuntublog/?p=114

grep -i '[^script$]' 1.txt
convert *.jpg output.pdf
2010-10-10 15:41:03
User: fazen
17

Given some images (jpg or other supported formats) in input, you obtain a single PDF file with an image for every page.

convert -density 300 input.pdf -fill "rgb(255,255,255)" -opaque "rgb(0,0,0)" output.pdf
2010-10-07 17:24:19
User: akrog
Tags: pdf print convert
2

The pdf is first converted to a bitmap, so change "-density" to match your printer resolution. Also be careful about the RAM required.

In this example rgb(0,0,0) is replaced by rgb(255,255,255), change to suit your needs.

evince "$(find -name 'NameOfPdf.pdf')"
2010-04-04 20:55:51
User: RBerenguel
-1

This assumes there is only one result. Either tail your search for one result or add | head -n 1 before the closing bracket. You can also use locate instead of find, if you have locate installed and updated

curl -sL xkcd.com | grep '<img [^>]*/><br/>' | sed -r 's|<img src="(.*)" title="(.*)" alt="(.*)" /><br/>|\1\t\2\t\3|' > /tmp/a; curl -s $(cat /tmp/a | cut -f1) | convert - -gravity south -draw "text 0,0 \"$(cat /tmp/a | cut -f2)\"" pdf:- > xkcd.pdf
2010-03-03 03:41:31
User: matthewbauer
Functions: cat cut grep sed
Tags: pdf xkcd caption
6

Saves to a PDF with title and alt text of comic.

As asked for on http://bbs.archlinux.org/viewtopic.php?id=91100

Change xkcd.com to dynamic.xkcd.com/comics/random for a random comic.

convert in.pdf out.jpg
passpdf(){ for i; do qpdf --password=<YOUR PASSWD> --decrypt "$i" "new$i"; done; }
2010-02-25 01:21:00
User: sputnick
Tags: pdf qpdf
-2

You should install qpdf.

That way, you can have a copy without any password required.

pdftotext [file] - | grep 'YourPattern'
2010-02-14 21:42:35
User: drewk
Functions: grep
Tags: pipe grep pdf
27

PDF files are simultaneously wonderful and heinous. They are wonderful in being ubiquitous and mostly being cross platform. They are heinous in being very difficult to work with from the command line, search, grep, use only the text inside the PDF, or use outside of proprietary products.

xpdf is a wonderful set of PDF tools. It is on many linux distros and can be installed on OS X. While primarily an open PDF viewer for X, xpdf has the tool "pdftotext" that can extract formated or unformatted text from inside a PDF that has text. This text stream can then be further processed by grep or other tool. The '-' after the file name directs output to stdout rather than to a text file the same name as the PDF.

Make sure you use version 3.02 of pdftotext or later; earlier versions clipped lines.

The lines extracted from a PDF without the "-layout" option are very long. More paragraphs. Use just to test that a pattern exists in the file. With "-layout" the output resembles the lines, but it is not perfect.

xpdf is available open source at http://www.foolabs.com/xpdf/

gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=OUTPUT.pdf -c .setpdfwrite -f INPUT.pdf
2009-12-14 21:30:22
User: deijmaster
Functions: gs
39

Remove security from PDF document using this very simple command on Linux and OSX. You need ghostscript for this baby to work.