Date created: 02/28/12 21:50:34. Last modified: 11/19/18 21:20:17

Random Commands

Fun / Misc:

Parrot Party:

curl parrot.live

The Weather:

curl wttr.in/

Digital clock in the terminal:

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

Fork Bomb:

#/bin/bash
$0&
$0

Change BASH prompt colour based on exit code of previous command:

PS1='`
  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\]\$";
  fi`\[\033[0m\]'

Bash progress bar/indicator/spinner:


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

# Or...

for((i=0;i<120000;i++))
do
echo -ne "Progress: $[ ($i+1)*100/120000 ]%\r";
done;

# 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

 

vi commands:

Go to end of file

G

Go to beginning of file

gg

Show line numbers

:set nu

Hide line numbers

:set nu!

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

dgg

Delete all lines above the current one

kdgg

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

dG

Delete all lines containing deleteme

:g/deleteme/d

 

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

or
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
segment100_2296000_av.ts?null=0
segment101_2296000_av.ts?null=0
segment102_2296000_av.ts?null=0
segment10_2296000_av.ts?null=0

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

$ ls -1
segment100_2296000_av.ts
segment101_2296000_av.ts
segment102_2296000_av.ts
segment10_2296000_av.ts

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