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 - 53 results
curl 'LINK' | pdftotext - - | less
2014-06-21 00:47:02
User: snipertyler
Tags: less pdf convert
4

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
2

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
2

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
0

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
3

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
0

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
3

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
2

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
0

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
0

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
0

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
19

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
0

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
0

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

ls -1 $PATH*/* | xargs file | awk -F":" '!($2~/PDF document/){print $1}' |xargs rm -rf
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