Terminal Command Line Tricks

Show size of directories in current

du -sh *

Delete all in current directory

rm * 

Terminal Countdown Clock

Set the time at the MIN variable; in this case, MIN is 1 minute

MIN=1 && for i in $(seq $(($MIN*60)) -1 1); do printf "\r%02d:%02d:%02d" $((i/3600)) $(( (i/60)%60)) $((i%60)); sleep 1; done

This will set a countdown to two hours (MIN is 120 minutes)

MIN=120 && for i in $(seq $(($MIN*60)) -1 1); do printf "\r%02d:%02d:%02d" $((i/3600)) $(( (i/60)%60)) $((i%60)); sleep 1; done

And this will set a countdown to 30 seconds (MIN in 0.5)

MIN=0.5 && for i in $(seq $(($MIN*60)) -1 1); do printf "\r%02d:%02d:%02d" $((i/3600)) $(( (i/60)%60)) $((i%60)); sleep 1; done

Display Terminal history

history

Run a command from history

Get the command-number by running history and then type it with an exclamation mark

!5234 example

Delete directories in current despite warning

rm -rf directory_name
Striping quotes from a large file
sed 's/\"//g' 29935_EDIT.txt > new.txt

Create multiple named files

This creates 0 to 9 files with leading zeroes ie 02_example.html

touch 0{0..9}_expample.html;

This creates 10 to 20 files named 10_example.html

touch {10..20}_example.html

Run in Terminal to create files from 0 to 20:

touch 0{0..9}_example.html; touch {10..20}_example.html

Rename

A quick terminal rename. Here I’m adding ā€œ30281_Rxl_ā€ to all files within a folder

zmv '(*)' '30281_Rxl_$1'

A find & replace

This renames all .txt files to .markdown

zmv -W '*.txt' '*.markdown'

Find & Replace within a filename

In a folder with a file named example.txt ex will be replaced with hello following this pattern: s/FIND/REPLACE/g

ls | while read item; do mv $item $(echo $item|sed -e s/ex/hello/g); done
Find with termimal

This looks for filenames containing 2563:

ls -c -ltd *2563*

Find only in where you are

This looks for filename containing .blade only in the directory where you already are. (In this case, I would be in a Laravel directory).

mdfind .blade -onlyin .

Remove spaces

for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done

This will also remove spaces:

find . -name '* *' | while read file; do target=`echo "$file" | sed 's/ /_/g'`; mv "$file" "$target"; done;

Rename .jpeg to .jpg

for f in *.jpeg; do 
mv -- "$f" "${f%.jpeg}.jpg"
done

Syncing folders

In this case, I want to sync the directory Docs at /Volumes/External01 to /Volumes/Exteral02/Docs. So, cd to base directory (External01) and rsync -azP DIRECTORY TARGET DIRECTORY --delete

The -P flag shows progress and --delete deletes on the target if things have been moved to prevent duplication.

/Volumes/External01 $> rsync -azP Docs/ /Volumes/External02/Docs --delete

Date / Calendar

Display calendar for October 1583

cal 10 1583

Date 18 months ago:

date -v -18m

Date 18 months from now

date -v +18m

Date 3 weeks ago

date -v -3w

Date 3 weeks ahead

date -v +3w

UTC Time at present

date -u

Encryption

Hash a word or phrase

This hashes the word Bitcoin! to 256:

$> echo -n Bitcoin! | shasum -a 256 | awk '{print $1}'
$> b81bf654dc352f66a0fcb51da8a260b60cf4a92dcf01f04f5f523961d9f6a6d7

This encrypts the phrase to 256:

 ~  echo -n obscurum per obscurius, ignotum per ignotius | shasum -a 256
$> 6c62181f 64764c19841025e64d0af46d88e682da1e61a9d1d5037d1fc4424c7a

This encrypts a png to a text file

openssl base64 -in sc_2018-04-03_a.png -out a.txt

This decrypts the text file back to a png

openssl base64 -d -in a.txt -out a1.png
Take a file and encrypt it with a password

In this case, we are encrypting text.html with the password Geesh:

openssl enc -aes-256-cbc -salt -a -in text.txt -out text.txt.enc -k Geesh

Decrypt to file text_decrypted.txt:

openssl enc -aes-256-cbc -d -a -in text.txt.enc -out text_decrypted.txt -k Geesh

HTML

This extracted the CSS at top of an HTML file to its own file:

text="$(sed 's:^ *::g' < Ex_E.html | tr -d \\n)" ; 
style=$(sed 's:.*<style type="text/css">\([^<]*\)<.*:\1:' <<<"$text") ; 
style2=$(sed 's/;}/\;}\\\n/g'  | <<< "$style");
style3=$(sed 's/*\//\*\/\\\n/g' <<< "$style2");
echo "<style type=\"text/css\">\n" "$style3" "\n</style>"  > style.css
Make a basic .html page to display images in a folder
for i in `ls *.jpg`; print "<img src='$i' width='100'/>" >> test.html