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.

Universal configuration monitoring and system of record for IT.

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:



May 19, 2015 - A Look At The New Commandlinefu
I've put together a short writeup on what kind of newness you can expect from the next iteration of clfu. Check it out here.
March 2, 2015 - New Management
I'm Jon, I'll be maintaining and improving clfu. Thanks to David for building such a great resource!

Top Tags





Commands tagged pdf from sorted by
Terminal - Commands tagged pdf - 56 results
input=a.pdf ; pages=`pdftk $input dump_data | grep -i numberofpages | cut -d" " -f 2`; pdftk A=$input shuffle A1-$[$pages/2] A$pages-$[$pages/2+1] output "${input%.*}.rearranged.${input##*.}"
2015-04-26 20:05:20
User: kobayashison
Functions: cut grep

Rearrange pdf document coming from a simplex document feed scanner, feeded first with odd pages, then with even pages from the end. Needs pdftk >1.44 w/ shuffle.

Similar to http://www.commandlinefu.com/commands/view/7965/pdf-simplex-to-duplex-merge where there are 2 separate documents, odd and even

pdftk fill_me_in.pdf output no_thanks.pdf flatten
2014-09-30 09:59:46
User: qdrizh

Some PDF viewers don't manage form fields correctly when printing. Instead of treating them as transparent, they print as black shapes.

pdfjam --clip true --trim '10mm 11cm 22pts 0' m.pdf
2014-07-08 10:57:54
User: qdrizh
Functions: true

This will crop each page of the PDF by 10mm left, 11cm bottom, 22pts right, and nothing from the top.

curl 'LINK' | pdftotext - - | less
2014-06-21 00:47:02
User: snipertyler
Tags: less pdf convert

Probably will not work very well with scanned documents.

for file in *.pdf; do convert -verbose -colorspace RGB -resize 800 -interlace none -density 300 -quality 80 "$file" "${file//.pdf/.jpg}"; done
2014-06-19 15:52:42
User: malathion
Functions: file
Tags: pdf convert

Without the bashisms and unnecessary sed dependency. Substitutions quoted so that filenames with whitespace will be handled correctly.

pdftk *.pdf cat output merged.pdf
2014-03-02 01:53:37
User: o0110o
Functions: cat

Merge Multiple PDFs In Alphabetical Order

pdfunite 1.pdf 2.pdf 3.pdf result.pdf
convert multi_layer.tif -compress jpg multi_page.pdf
2013-09-26 14:44:01
User: algol

Creates a PDF file where each page will be a layer from de original TIFF file.

You can apply many other filters and transformations.

convert multi_layer.tif -page a4 -compress jpg multi_page.pdf

To set the page size

convert multi_layer.tif -crop 590x790+20+30 -compress jpg multi_page.pdf

To include only a portion of the image (discard first horizontal 20 pixels and first vertical 30 pixels, include the next 590 horizontal and 790 vertical pixels)

convert multi_layer.tif -delete 1,3,5-10 -compress jpg multi_page.pdf

Discard mentioned layers

pdfunite 1.pdf 2.pdf united.pdf
2013-07-09 11:44:58
User: ichbins
Tags: pdf

pdfunite is a part of the poppler-utils. poppler-utils package is only 150KB. The alternative - pdftk package is 14MB! Install poppler-utils if you need simple pdf operation commands like unite, separate, info, text/html conversions

for i in *.pdf; do echo --------$i-------; echo; pdftotext $i - | grep -i Yourpattern; done
2013-05-22 05:36:06
User: fangfufu
Functions: echo grep
Tags: grep pdf

This command is useful for searching through a whole folder worth of pdf files.

scanimage -p --resolution 250 --mode Gray -x 215.9 -y 279.4 | pnmtops -imageheight 11 -imagewidth 8.5 | ps2pdf - output.pdf
2013-04-12 18:18:42
User: dbh
Functions: scanimage
Tags: pdf scan scanner

Adjust the --resolution and --mode as required (if these options are available for your scanner).

The size options (-x, -y, -imageheight, -imagewidth) are for US letter paper. For A4, I think the command would be:

scanimage -p --resolution 250 --mode Gray -x 210 -y 297 | pnmtops -imageheight 11.7 -imagewidth 8.3 | ps2pdf - output.pdf
qpdf --decrypt inputfile.pdf outputfile.pdf
2013-04-06 10:20:09
User: em

Remove security restrictions from PDF documents using this very simple command on Linux and OSX. You need QPDF installed (http://qpdf.sourceforge.net/) for this to work.

convert {1..12}.png MyMultiPageFile.pdf
2013-03-26 01:30:38
User: lpanebr

Creates a PDF from multiple images. One page per image.

If you want a specific arbitrary order you can use {1,3,5,10,12}

* you may use jpg, tif etc

** if you do use jpg images you might want to add "-compress Zip" as suggested below to prevent from having the images from being re-compressed.

find . -name "*.pdf" -exec pdftk {} dump_data output \; | grep NumberOfPages | awk '{s+=$2} END {print s}'
pdf2jpg(){ convert -density 400 -bordercolor white -border 0 -resize 992x1403 -quality 85 $1 ${1/%pdf/jpg} ; }
pdftk A=1.pdf B=2.pdf C=3.pdf cat C A output CA.pdf
pdftk 1.pdf 2.pdf cat output 12.pdf
pdfinfo file.pdf | grep "^Pages: *[0-9]\+$" | sed 's/.* //'
pdftk file.pdf dump_data output | grep -i Num
gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf
2012-02-29 21:30:20
User: DavideRiboli
Functions: gs

Useful if you want to reduce PDF file size using command line by ghostscript.

enscript -L1 -b'||Page $% of $=' -o- < <(for i in $(seq "$(pdftk "$1" dump_data | grep "Num" | cut -d":" -f2)"); do echo; done) | ps2pdf - | pdftk "$1" multistamp - output "${1%.pdf}-header.pdf"
2012-01-27 12:26:33
User: captaincomic
Functions: seq

Put this code in a bash script. The script expects the PDF file as its only parameter.

It will add a header to the PDF containing the page numbers and output it to a file with the suffix "-header.pdf"

Requires enscript, ps2pdf and pdftk.

echo "This text gets stamped on the top of the pdf pages." | enscript -B -f Courier-Bold16 -o- | ps2pdf - | pdftk input.pdf stamp - output output.pdf
2012-01-03 14:58:10
User: svg
Functions: echo

To quickly add some remark, comment, stamp text, ... on top of (each of) the pages of the input pdf file.

find -iname "*.pdf" -exec pdfinfo -meta {} \;|awk '{if($1=="Pages:"){s+=$2}}END{print s}'
2011-12-13 15:02:11
User: Barabbas
Functions: awk find
Tags: awk find pdf count sum

This sums up the page count of multiple pdf files without the useless use of grep and sed which other commandlinefus use.

pdfcrack <FILE>
for F in *.pdf ; do qpdf --password=your_password --decrypt "$F" "$(basename $F .pdf)-nopw.pdf" ; done
2011-11-30 21:35:33
User: slc66

This command line will remove password from all PDF files in the current folder. It use qpdf.