Date created: 02/28/12 21:50:34. Last modified: 06/24/19 09:13:33

Random Commands

Fun / Misc:

Parrot Party:



The Weather:



Digital clock in the terminal:

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


Fork Bomb:



Another Fork Bomb:

:(){ :|:& };:


Change BASH prompt colour 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\]\$";


Bash progress bar/indicator/spinner:

tar -cvzf ${TAR} ${DIR} | tqdm --unit_scale --total $(find ${DIR} -type f | wc -l ) > /dev/null

# Or...

echo -ne "Progress: $[ ($i+1)*100/120000 ]%\r";

# Or...

while :;do for s in / - \\ \|; do printf "\r$s";sleep .1;done;done


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


find commands:

Find and delete Mac .DS-Store and ._.DS_Store files:

# Using "\;" calls rm for every file found
find . -name "*.DS_Store" -exec rm {} \;

# Using "+" calls rm once with all file names as arguments to speed up the process
find . -name "*.DS_Store" -exec rm {} +

# When working with external commands find output can be piped to xargs verbatim...
find . -name "*.DS_Store" | xargs ...

# Or use -print to print each file/path separated by a new line or -print0 to print each file/path separated by a space character
find . -name "*.DS_Store" -print0 | xargs ...

# If files have spaces in the name/path, find can delete them using the -delete option
find . -name "*.DS_Store" -delete


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 %



alias timer='echo "Timer started. Stop with Ctrl-D." && date && time cat && date'


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 {} \;


Rename video segments to remove trailer then join them together:

$ ls -1

$ rename 's/\?null=0//' *

$ ls -1

$ ffmpeg -i "concat:segment1_2296000_av.ts|segment2_2296000_av.ts|segment3_2296000_av.ts" -c copy -bsf:a aac_adtstoasc output.mp4