Skip to content

Instantly share code, notes, and snippets.

@Diablo2050
Created February 8, 2021 17:05
Show Gist options
  • Save Diablo2050/5db62870c0455d67e2539d623f82da4b to your computer and use it in GitHub Desktop.
Save Diablo2050/5db62870c0455d67e2539d623f82da4b to your computer and use it in GitHub Desktop.
terminal-essentials

1- System Information


When we are dropped in a terminal of a server we will need to gather basic information on the system using the below commands:

1- How to know the which OS we are in ?

cat /etc/os-release

2- How to know which kernel we are using ?

uname -r

3- How much RAM does the system has ?

free -h

4- CPU type and how many cores ?

lscpu

5- Disk and Storage info ?

lsblk

df -h

6- What's my IP Address ?

ip a

curl ifconfig.me

dig +short myip.opendns.com @resolver1.opendns.com

To access remote systems we need user, password or user and ssh key

1- To create a new ssh key:

ssh-keygen

To create a new ssh key with a different path run the above command and supply the path to the file.

2- To access the system with the key we generated:

ssh -i /path/to/file user@host -p port

3- ssh config file can be used to organise our remote servers like so :

Host server1 

    HostName 1.2.3.4 
    Port 22 
    User ubuntu 
    IdentityFile ~/.ssh/key.pem 

2- File and Folder manipulation


1- How to create files ?

touch filename

touch file{1,2,3,4}

touch dir{1,2,3,4}

mkdir code

mkdir code/main/app -p

fallocate -l 1G file.txt

echo "Hello" > file.txt

2- How to list files ?

command description
ls short listing
ls -l long listing
ls -a show hidden files
ls -lt list by date

3- How to merge several commands together ?

ls -lat | head

4-Navigate lots of files in a directory ?

ls -l | head -n 40

ls -l | tail -n 40

5- basic navigation:

/home/username/app = ~/app

cd /home/amr/app

cd ~/app

cd -

to return to the previous location

6- To sort files or directories :

ls -l | sort

7- remove duplicate files or directories ?

ls -l | sort | uniq

8- find certain files or directories ?

find /path/ -type f -iname code.txt

execute command after find:

find . -type d -exec ls -l {} \;

9- find patterns in files :

grep -i PATTERN FILE

To counnt the number of lines :

grep -i cron /var/log/syslog | wc -l

9- To properly view logs:

tail -f /path/to/log/file

less /path/to/log/file

3- Process and Port related commands


1- To view running processes with auto refresh:

top

2- To print out running processes:

ps aux

ps ajxf

3- To view currently listening ports:

ss -tulpn

4- Misc.

1- Print out path permission :

namei -l /var/log/syslog

2- What does chmod 755 mean ?

item description numeric value
r read access 4
w write access 2
x execution access 1

Permissions are evaluated as follows :

drwxr-xr-x

d: indiated directory, if blank means file

rwx: are owner permissions

r-x: are group permissions

r-x: are others permision

3- To get the size of a directory :

du -sh /path/to/dir

better is to install ncdu package

ncdu -x /

4- soft link and hard link :

soft link is like a shortcut

ln -s TARGE LINK_NAME

Hard link is making an exact copy of something, but it doesn't take extra disk space

ln TARGE LINK_NAME

5- search for a keyword in a directory :

grep -irl /path/ -e "keyword"

6- copy files around:

cp -ar dir1 dir1_copy

rsync is widely used to transfer files

rsync -avhHp source/ destination/

rsync -avhHp -e "ssh -p22" source/ destination/

v: verbose

h: output is human readable

H: preserve hard links

p: preserve permissions

e: can supply ssh specific options

for testing use --dry-run

7- Use .bashrc to execute commands at shell login

echo "welcome $USER"

8- history is used to display terinal history for a user

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment