Random Commands

Fun / Misc:

Digital clock in the terminal

watch -t -n 1 "date +%T | figlet"

Fork Bomb


Change BASH prompt colur based on exit code of previous command

  if [ $? -eq 0 ];
    then echo -n "\[\033[00;35m\]\u\[\033[01;32m\]@\[\033[00;35m\]\h\[\033[00;32m\](\[\033[01;35m\]\W\[\033[01;32m\])\[\033[00;32m\]\$";
    else echo -n "\[\033[00;35m\]\u\[\033[01;31m\]@\[\033[00;35m\]\h\[\033[01;31m\](\[\033[35m\]\W\[\033[31m\])\[\033[00;31m\]\$";

dd commands:
Dump CD to ISO

dd if=/dev/cdrom of=/tmp/cdimg1.iso


Generate 10GBs

dd if=/dev/zero of=/tmp/image bs=1M count=10000


To view dd progress, use a seperate terminal:

$ pgrep -l '^dd$'
4858 dd
kill -USR1  4858

The progress will be dumped in terminal window where dd is running. Use watch to have continuous output:

watch -n 60 'sudo kill -USR1 4858'

To compress a drive copy:

dd if=/dev/hdb | gzip -c  > /image.img

To restore a compressed drive copy:

gunzip -c /image.img.gz | dd of=/dev/hdb

Zero out free space on a drive before copying into gzip for better compression:

mkdir /mnt/hdb
mount /dev/hdb /mnt/hdb
dd if=/dev/zero of=/mnt/hdb/zero
rm /mnt/hdb/zero
umount /mnt/hdb
dd if=/dev/hdb | gzip -c  > /image.img

Faster copy:

bs=100M conv=notrunc


vi commands:

Go to end of file


Go to beginning of file


Show line numbers

:set nu

Hide line numbers

:set nu!

Delete everything from the current line to the top of the file


Delete all lines above the current one


Delete everything from the current line to the end of the file


Delete all lines containing deleteme



System commands:
Scrollable full `ps` output

ps awwfux | less -S

Find out how much data is waiting to be written to disk
grep ^Dirty /proc/meminfo

Release memory used by the Linux kernel on caches

free -m && sync && echo 3 > /proc/sys/vm/drop_caches && free -m

Handy tricks:

Seconds to Hours:Minutes:Seconds

awk -v seconds=54321 'BEGIN {print strftime("%H:%M:%S", seconds, 1)}'


Epoch time to human time

date -d @1331485023


Sort du output by size

du -hs | sort -rh


Echo with sudo

sudo 'echo "10" > /proc/sys/vm/swappiness'      # Doesn't work
sudo sh -c 'echo "10" > /proc/sys/vm/swappiness' # Works for replace, >> for append
echo "10" | sudo tee -a /proc/sys/vm/swappiness > /dev/null # Works for append, remove -a for replace


Recursive grep

grep -r "string" /blah/blah


Sort ls output by reverse modified time (so newest at bottom)

ls -ltr

Sort by size (smalllest at bottom)

ls -ls
ls -lS 


Start COMMAND, and kill it if still running after 5 seconds

timeout 5s COMMAND


Write in vim without sudo

:w !sudo tee %


Music & Media Commands

Split flac file with cue file

sudo apt-get install cuetools shntool flac
shnsplit -f file.cue -t %n-%t -o flac file.flac

Convert from flac directly to 320Kbps MP3

# sudo apt-get install lame ffmpeg libavcodec-extra-53

for f in *.flac; do ffmpeg -i "$f" -acodec libmp3lame -ab 320k "${f%.flac}.mp3"; done

find -name "*.flac" -exec ffmpeg -i {} -acodec libmp3lame -ab 320k {}.mp3

Decompress from flac to wav, then encode to mp3 320k

mkdir temp
flac -d *.flac
for f in *.wav; do lame -b 320k "$f" ./temp/"${f%.wav}.mp3"; done
rm *.wav

Convert mp4 video to mp3 audio only:

# -i
# -acode 
# -ac 
# -b:a 
# -ar 
ffmpeg -i Input-Video.mp4 -acodec libmp3lame -ac 2 -b:a 320k -ar 48000 Output-Audio.mp3

for file in *.mp4; do ffmpeg -i "$file" -acodec libmp3lame -ac 2 -b:a 320k -ar 48000 "`basename -s ".mp4" "$file"`.mp3"; done

Recursively unrar archives in multiple sub-folders

find . -name '*.rar' -execdir unrar e {} \;