My Unix Command ScratchPad

Copying public key from local machine to a remote host:

Use the ssh-copy-id tool to do this.
To install this tool in Mac use brew install ssh-copy-id.

ssh-copy-id user@hostname  

It will prompt for the user's password. After giving the password it will automatically add your local machine's publicy-key to the remote hosts authorized-keys file.

Checking the disk space in your remote host

df -h  


Checking the size of a directory

du -sh directory_name  

Source: stackexchange

Recursively giving an extension to all the files in a directory

find . -type f -exec mv '{}' '{}'.jpg \;  

Source: Stackoverflow

Another solution for bulk change of filename

find . -name '*.tar.gz' -exec sh -c 'mv "$0" "${0%.tar.gz}.gzip"' {} \;  

Incrementally backing up contents of a directory to another directory

cp -r -n /Users/arefindk/Pictures/* /Volumes/MISC/mac_syed_photo_backup/  

Securely copy from a server

scp user@remotehostaddress:/desired/directory/file.tar.gz /Users/username/Desktop/

Securely copy into a server

scp /Users/username/Desktop/baz.tar.gz user@remotehostaddress:/desired/directory/  

Adding subl command for sublime text 2 in Mac terminal

ln -s /Applications/Sublime\ Text\ /usr/local/bin/subl

To find a string in a file and then show the surrounding lines in unix

cat filename | grep -A 100 -B 40 searchstring  

Here -A means how many lines to show after and -B means how many lines to show before.

Create SSH tunnel to forward a localhost port

ssh -nNT -L  

Source: Trackets Blog

Pretty print a json gzipped file

zcat out.json.gz | head -1 | python -m json.tool  


Watch filesize live

watch -n 5 "du -h relationship_000993aa-7d13-4a77-a638-25ddfa362ba0.csv"  

Source: Stack Overflow

Show Comments