Index

Table of contents

File Conversions

mime types

get the mime type of a file (guestimate)
file -i [file]

documents

imagemagick

install imagemagick convert
sudo apt-get install imagemagick
update security policy
sudo vim /etc/ImageMagick-6/policy.xml
<policy domain="coder" rights="read|write" pattern="PDF" />
convert pdf to bmp
convert [name].pdf [name].bmp
convert pdf to jpg
convert -density 600 [name].pdf [name].jpg
zero pad filenames
convert -density 600 [name].pdf [name]-%02d.jpg

qpdf

remove password from pdf
qpdf --password=[password] --decrypt [pdf] [output_file]

calibre

install calibre convert
sudo apt-get install calibre
epub to text file
ebook-convert [file] [output]
supported formats list
https://manual.calibre-ebook.com/faq.html#what-formats-does-calibre-support-conversion-to-from

images

install imagemagick convert
sudo apt-get install imagemagick
rotating images
convert "$name" -rotate 180 "rotated-$name"
append images next to each other
convert +append a.jpg b.jpg line.jpg
append images above each other
convert -append a.jpg b.jpg column.jpg
crop an image
convert chart.png -crop 1200x700+20+10 chart-cropped.png
split an image into tiles
convert chart.png -crop 64x64 tile.png

video

install ffmpeg
sudo add-apt-repository ppa:mc3man/trusty-media
sudo apt-get update
sudo apt-get install ffmpeg
re-encode to another file format
ffmpeg -i source.mp4 output.webm
re-encode with a bitrate of 350.000 (to shrink file)
ffmpeg -i source.mp4 -b 350000 destination.mp4
re-encode with a single frame per second (to shrink file)
ffmpeg -i source.mp4 -r 1 destination.mp4
re-encode all files in one folder to another folder
find -type f -exec ffmpeg -i {} -r 1 ../converted/{} \;
extract a chunk from a video file
ffmpeg -i [input file] -ss 0.50 -to 1 [output file]
ffmpeg -i [input file] -ss 1 -to 2 [output file]
ffmpeg -i [input file] -ss 1:02 -to 2:04 [output file]
ffmpeg -i [input file] -ss 0:01:02 -to 0:02:04 [output file]
ffmpeg -i [input file] -ss 0:01:02.00 -to 0:02:04.00 [output file]
merge 2 mkv files
sudo apt-get install mkvtoolnix
mkvmerge -o [output file] [file1] +[file2]

audio

decode m4a to wav
faad [name].m4a
decode mp3 to wav
ffmpeg -i [name].mp3 [name].wav
encode wav to ogg
oggenc [name].wav
concatenate wav files
sox file1.wav file2.wav output.wav
store mp3 stream (e.g. internet radio) to a file
streamripper [url] -a [file]
curl --output [file] [url]
extract a chunk from an mp3
mp3splt [file] [minutes].[seconds](.[millis])? [minutes].[seconds](.[millis])?
split a mp3 file into 50 minute chunks
mp3splt [file] -t 50.00

Encoding

base 64 encode

echo user:password | base64
base 64 decode
echo Z2VuaWFsZSBzaGl0DQo= | base64 --decode
hex decode
echo 68697070652073686974210d0a | xxd -r -p

md5

cat [file] | md5sum

sha1

calculate sha1 hash for file
sha1sum $file

json

pretty format json
echo '{"a":"b", "c":"d"}' | python -m json.tool
echo '{"a":"b", "c":"d"}' | jq .
call url and pretty format returned json
curl http://example.com/path -H "Content-Type: application/json" | python -m json.tool
curl http://example.com/path -H "Content-Type: application/json" | jq .
pretty format contents of file
cat [file] | python -m json.tool
jq . [file]
extract value of key "a"
echo '{"a":"b", "c":"d"}' | jq .a
extract first array element
echo '["a", "b"]' | jq '.[0]'
extract the "message" (object or value) contained in every array entry of the contents object
jq ".contents[] | .message"
For every element in an array extract the address object, but only if the .address.country is set to "BE"
jq '.[].address | select(.country == "BE")'
For every element in an array extract the address.city, but only if the .address.country is set to "BE"
jq '.[].address | select(.country == "BE") | .city'
for element parent append the values 'first' and 'second'
jq '.parent | "\(.first) \(.second)"'
reverse an array
jq 'reverse'
sort an array on the contained object's "name" property
jq '. | sort_by(.name)'
reverse sort array
jq '. | sort_by(.name) | reverse'
jq manual: https://stedolan.github.io/jq/manual/

html

format html
[cmd] | tidy
only output html
[cmd] | tidy -q
indent html tags
[cmd] | tidy -i
no line wrapping
[cmd] | tidy --wrap 0
hide info messages
[cmd] | tidy --show-info no
hide warnings
[cmd] | tidy --show-warnings no
hide errors (6=default)
[cmd] | tidy --show-errors 0
example: pretty formatting
[cmd] | tidy -iq --wrap 0 --show-errors 0

xml

pretty format
xmllint -format -recover [file]
select an xml element with its contents
xmlstarlet sel -t -c '//[element]'
select the value of an xml attribute
xmlstarlet sel -t -v '//[element]/@[attribute]'
namespace example: extract all artifactId's from a pom
xmlstarlet sel -N n="http://maven.apache.org/POM/4.0.0" -t -v '//n:artifactId' -n pom.xml
url encode
encoded=$(python -c "import urllib; print urllib.quote('''$url''')")