handy linux commands

When I need to get my computer to do a thing, I use computer code. This page is my reference sheet for when I need to do that one thing I did a year ago but I can't remember how I did it exactly, or this really obvious thing that is somehow only described in a single forum post from 2002 - who knows, it may be of some use to you.

It mostly involves either software that come pre-installed with most Linux distros or is available from the usual repos.

renaming files

Make filenames lowercase for all files in working directory:
rename 'y/A-Z/a-z/' *
Capture strings in filenames and reuse them:
rename -n -e 's/(\d+)randomcrap(\d+)/$1randomcrap$2/' *
Remember to \escape symbols like ., # or !. To preview the result without renaming, add -n directly after rename.

Remove preceding zeroes from filenames:

rename 's/^0*//' *
Take all first lines from every .txt file in the working directory and put those lines in newfile.txt:
read -n1 *.txt > newfile.txt
Replace every occurence of the string 'foo' with the filename of the file it occurs in:
for f in *.txt
 name=$(basename "$file")
 sed -i -e "s/foo/$namex/g" $name
Remove any line in file.txt that does not contain the string 'foo':
sed -n '/foo/p' file.txt > newfile.txt

media conversion

Convert file to ogg audio, compression level 5:
ffmpeg -i in.* -acodec libvorbis -vn -q:a 5 out.ogg
Convert file to mp4 video, quality level 19:
ffmpeg -i in.* -c:v libx264 -crf 19 -strict experimental out.mp4
Change volume in video to audible levels:
ffmpeg -i in.* -vcodec copy -af "volume=100dB" out.*
Remove 10 seconds from start of file, output as ogg audio, compression level 5:
ffmpeg -i in.* -acodec libvorbis -ss 00:00:10 -vn -q:a 5 out.ogg
Convert video for Web use (webm):
ffmpeg -i in.* -vf scale=640:ih*640/iw -vcodec libvpx -b:v 0.4M -acodec libvorbis -qscale:a 5 out.webm
Convert video for Web use if original video height is an uneven number of pixels:
ffmpeg -i in.* -vf scale=640:1+ih*640/iw -vcodec libvpx -b:v 0.4M -acodec libvorbis -qscale:a 5 out.webm
Turn series of images into MP4 video (0001.tif, 0002.tif...)
ffmpeg -r 60 -f image2 -s 1004x1024 -i %04d.tiff -vcodec libx264 -crf 25 -pix_fmt yuv420p out.mp4
Reduce gamma of video:
ffmpeg -i in.* "eq=gamma=0.7" out.*

Extract subtitles from mkv video

You'll need to install MKVtoolnix. First, check which MKV track the subtitles are in:
mkvmerge -i input.mkv
Then extract the track:
mkvextract tracks input.mkv [trackID]:[output].srt
Example, if subtitles are in track 2:
mkvextract tracks movie.mkv

remove mkv metadata title

MKV files (films, series) are sometimes given very unhelpful titles. I have VLC set to display the metadata title and I hate not being able to see what episode of something I'm on - this takes quick care, editing files in place to delete the title tag. This removes the title from all mkv files in the working directory; it requires MKVtoolnix.
for f in *.mkv; do mkvpropedit -d title "$f"; done

split flac audio according to cue sheet

FLAC audio can come in a single file, which is then played according to a cue sheet, so a music player displays it as a set of tracks. Great, but I use a file manager that just sees a great big file. If you have the cue file and the FLAC file, install shnsplit and run this (change filenames of course) to split the file into individual FLAC files:
shnsplit -f yourfile.cue -t %n-%t -o flac yourfile.flac

combine vob dvd files into a single video file

This command combines DVD VOB files into a single VOB video file. Add or remove numbers from between the square brackets depending on how many files you have, and what they're called.
cat VIDEO_TS/VTS_01_[1234].VOB > vid.vob

some deja-dup/duplicity stuff

Restore specific file or directory in working directory:
deja-dup --restore FILE1 FILE2
Generate full list of all files in the current backup, write to file 'bakfiles':
duplicity list-current-files file:///path/to/backup/folder > bakfiles

documents & misc

Convert all .doc files in working directory to PDFs of the same filenames:
lowriter --convert-to pdf *.doc
Compress images in PDF files and output a new PDF file. This particular command compresses embedded images to 'ebook' levels; decent but not beautiful.
gs -sDEVICE=pdfwrite -dNOPAUSE -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dBATCH -sOutputFile=out.pdf in.pdf
The controls aren't terribly fine-grained, but it's good enough for most purposes. Other options for the -DPDFSETTINGS flag are /screen (shit), /printer (a bit better than ebook), /prepress (nice) and /default (the best).

List all installed fonts: