So just to supplement the bit about man
a little:
apropos
, whatis
, and man
are of course the holy trinity of learning as you go on Unix and Unix-likes. You should also know about info
, which is basically the same as man
but some programs put their most detailed manuals in info
instead of man
pages.
There’s also a more recent man
-like program that you should know and learn to love called tldr
, which is like man
but instead of giving you all the deets, it tells you how to do a few of the most common things a command is used for, and it tells you in a really concise way.
This, for example, is the output of tldr
for pdftk
, a common command-line utility for manipulating PDF files en masse in scripts.
pdftk
PDF toolkit.More information: https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit.
- Extract pages 1-3, 5 and 6-10 from a PDF file and save them as another one:
pdftk {{input.pdf}} cat {{1-3 5 6-10}} output {{output.pdf}}
- Merge (concatenate) a list of PDF files and save the result as another one:
pdftk {{file1.pdf file2.pdf …}} cat output {{output.pdf}}
- Split each page of a PDF file into a separate file, with a given filename output pattern:
pdftk {{input.pdf}} burst output {{out_%d.pdf}}
- Rotate all pages by 180 degrees clockwise:
pdftk {{input.pdf}} cat {{1-endsouth}} output {{output.pdf}}
- Rotate third page by 90 degrees clockwise and leave others unchanged:
pdftk {{input.pdf}} cat {{1-2 3east 4-end}} output {{output.pdf}}
or for sed
, which we’ve discussed:
sed
Edit text in a scriptable manner.
- Replace the first occurrence of a regular expression in each line of a file, and print the result:
sed 's/{{regex}}/{{replace}}/' {{filename}}
- Replace all occurrences of an extended regular expression in a file, and print the result:
sed -r 's/{{regex}}/{{replace}}/g' {{filename}}
- Replace all occurrences of a string in a file, overwriting the file (i.e. in-place):
sed -i 's/{{find}}/{{replace}}/g' {{filename}}
- Replace only on lines matching the line pattern:
sed '/{{line_pattern}}/s/{{find}}/{{replace}}/' {{filename}}
- Delete lines matching the line pattern:
sed '/{{line_pattern}}/d' {{filename}}
- Print only text between n-th line till the next empty line:
sed -n '{{n}},/^$/p' {{filename}}
- Apply multiple find-replace expressions to a file:
sed -e 's/{{find}}/{{replace}}/' -e 's/{{find}}/{{replace}}/' {{filename}}
- Replace separator / by any other character not used in the find or replace patterns, e.g., #:
sed 's#{{find}}#{{replace}}#' {{filename}}
- Print only the n-th line of a file:
sed '{{n}}q;d' {{filename}}
Really, really, lovely if you’re CLI-curious but sometimes you feel a little overwhelmed working your way through all those manuals. Perfect when you just want a quick and dirty example to solve a common problem. You can grab it on Ubuntu-based distros with sudo apt install tldr