CS: Ubuntu Commands

 # Checking the current version of installed ubuntu OS:
lsb _release -a

 # checking disk space usage:
df -h

 # checking disk space usage in a directory
du -hs  *

 # changing the password of the user:
passwd  [username ]

 # listing the cronjobs:
crontab -l

 # editing cronjobs:
crontab -e

 # tailing logs
tail -f  [filePath ]

# tailing last 100 lines of the log file 
tail -f -n 100  [filePath ]

 # displaying contents of the file
cat  [filePath ]
more  [filePath ]
less  [filePath ]

 # showing only lines which have the string "testString"
grep "testString"  [file ]

# grep with OR
grep "pattern1  |pattern2"  [file ]

# extended grep (below does the same like grep with OR, however escaping characters is not required here)
grep -e "pattern1|pattern2"  [file ]

# egrep (same like grep -e, which is extended grep)
egrep 'pattern1|pattern2'  [file ]

# show location in terminal shell prompt (you can add this to ~/.bashrc)
export PS1='$(whoami)@$(hostname):$(pwd)'

Find where inodes are being used:

 # navigate to a specific directory
cd /
find . -printf "%h  n" | cut -d/ -f-2 | sort | uniq -c | sort -rn

FInd where the storage space is used

 # navigate to a specific directory
cd /
du -hs  *

mv /path/subfolder/{.,}* /path/

# copy files securely via ssh  [remote -> local ] (-r for recursive is applicable, -P for port)
scp -i test.pem ubuntu@hostname:/remote/location/path /destination/path

# copy files securely  [local -> remote ]
scp -i test.pem /destination/path ubuntu@hostname:/remote/location/path

TAR

Compress

tar -czvf /tmp/destination.tar.gz -C  [directory ]  [context _directory ]

Example:

tar -czvf /tmp/destination.tar.gz -C /tmp/where _to _compress .

Decompress / Extract

tar -xzvf /tmp/destination.tar.gz -C /tmp/where _to _extract .

Move all files including hidden to parent directory

mv /path/subfolder/{.,}* /path/

JSON Manipulations

# converting json to ini string
echo $json _var | jq -r "to _entries|map(  "  (.key)=  (.value|tostring)  ")|. [ ]"

How to use screen command

  • Start a new screen
screen
  • Detach from the screen
Ctrl + a + d
  • Attach to the screen
screen -rd
  • List Screens
screen -ls
  • Open the next screen to the current
Ctrl + a + n

Publish Date: 2014-10-04

Execute commands remotely using SSH

If you are accessing your SSH using public key authentication:

sudo ssh -i key.pemĀ ubuntu@host ‘command to be executed remotely’ ;

If you are accessing your SSH using username and password:

sudo ssh -u root -p ubuntu@host ‘command to be executed remotely’ ;