Home | About Me
| | | | | | | | | |

CS: Ubuntu Commands

Cheat Sheets | 04 Oct 2014
Tags: ubuntu, terminal, shell, 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 ```