Courses-Study

My notes for studying courses

View on GitHub

COMMAND LINE FUN

1 - THE BASH ENVIRONMENT

1.1 - ENVIRONMENT VARIABLES

$PATH : directories which bash searches in for commands
echo $PATH

$USER : the current user
$HOME : the current users home directory
export b=1.1.1.1 # make new global environment variable
b=1.1.1.1 # make new environment variable but only accessible in current shell
env # Show all environment varialbes

1.2 - TAB COMPLETION

ls D<tab>
Downloads/ Desktop/ Documents/

ls De<tab>
Desktop/

1.3 - BASH HISTORY TRICKS

# Show history commands
history

# Run command with the number associated
!<number> 

# Execute last command in our terminal session (like arrow key up and enter)
!!

2 - PIPING AND REDIRECTION

2.1 - REDIRECTING TO A NEW FILE

# Replace <file name> content with abcd, If the file does not exist it will be created
echo abcd > "file name"

2.2 -  REDIRECTING TO AN EXISTING FILE

# Append content to the existing file
echo abcd >> "file name"

2.3 -  REDIRECTING FROM A FILE

# Counts characters in a file
wc -m < filename.txt

2.4 - REDIRECTING STDERR

# Redirect standard error of command to error.txt
ls ./non_existing_file 2>error.txt

2.5 - PIPING

# Read file and pipes it to wc which counts characters of the file
cat filename.txt | wc -c
cat filename.txt | wc -c > count.txt 

3 - TEXT SEARCHING AND MANIPULATION

3.1 - GREP

ls -la /usr/bin | grep zip # Print lines which hash zip in it

# Case insensitive
grep -i

# Search Recursively
grep -r

3.2 - SED

# Replace harder with harder s means replace
echo "Try hard" | sed 's/hard/harder'
"Try harder"

3.3 - CUT

# Split text by ',' and prints second element
echo 'a,b,c,d' | cut -d ',' -f 2 
b

cut -d ":" -f 1 /etc/passwd
"usernames in /etc/passwd"

3.4 - AWK

echo "aaaa::bbbb::cccc" | awk -F "::" '{print $1 $3}'
aaaa cccc
# -F is field seperator and print $1 $3 extract first and third field

3.5 - PRACTICAL EXAMPLES

# Prints IP Addresses
cat access.log | cut -d ' ' -f 1 | sort -u

cat access.log | cut -d ' ' -f 1 | sort | uniq -c | sort -urn # Print IP Add

cat access.log | grep <IP> | cut -d '"' -f 2 | uniq -c

cat access.log | grep <IP> | grep '/admin' | sort -u

4 - EDITING FILES FROM THE COMMAND LINE

4.1 - NANO

nano <file name>

CTRL + O # Save changes
CTRL + K # Cut current like
CTRL + U # Paste cutted line at cursor location
CTRL + W # Search in the file
CTRL + x # Exit the file

4.2 - VI

vi <file name>

i in command mode # Enter insert mode
esc in insert mode # Exit insert mode and enter command mode
dd in command mode # Delete and cut current line
yy in command mode # Copy current line
p in command mode # Paste clipboard content
x in command mode # Delete current character
:w in command mode # Save file
:q! in command mode # Quit without saving
:wq in command mode # Save file and quit

5 - COMPARING FILES

5.1 - COMM

# Print unique lines in file1 in first collumn, uniqe lines inf file2 in second column and common files in third column
comm file1 file2

# Print common lines since we suppressed columns 1,2
comm -12 file1 file2

5.2 - DIFF

diff -c file1 file2 # Display result in context format 
# - indicates lines in first file not in the second
# + inficates lines in second file not in the first

diff -u file1 file2 # Display result in unified format 

5.3 - VIMDIFF

vimdiff file1 file2
CTRL + W + arrow key # Switch between windows
] + c # Jump to the next change
[ + c # Jump to the previous change
d + o # Get change from other windows and put it in current one
d + p # Get change from current windows and put it in other one
:q! # Quit vimdiff

6 - MANAGING PROCESSES

cat file | wc -c # Two processes but one job

6.1 - BACKGROUNDING PROCESSES (BG)

ping -c 400 localhost > ping_results.txt &
  1. We can suspend and stop execution of current foreground command with CTRL + Z
ping -c 400 localhost > ping_results.txt
CTRL+Z
  1. We can resume previous suspended command with bg command
bg # Runs and resume job in background

6.2 - JOBS CONTROL: JOBS AND FG

jobs # List jobs in current terminal session
fg %1 # Resume job number 1 and returns it in foreground
fg # Resume only one job and returns it in foreground

6.3 - PROCESS CONTROL: PS AND KILL

ps -ef
# -e displays all processes -f List all format listing

ps -fC <command name>
# -C command name
kill <PID>

7 - FILE AND COMMAND MONITORING

7.1 - TAIL

sudo tail -f /var/log/apache2/access.log
# -f continuously update the tail output in real time

tail -nx filename # Prints last x lines of the file

7.2 - WATCH

watch -n <seconds> <command> # Runthe specified <command> in every <n> seconds
# Use CTRL + C to exit it and return to the terminal

8 - DOWNLOADING FILES

8.1 - WGET

# Downloads file from URL and saves it in different name by -O (capital case o) option
wget -O filename <URL LINK>

8.2 - CURL

# Downloads file from URL and saves it in different name by -o (lower case o) option
curl -o filename <URL LINK>

8.3 - AXEL

axel -a -n 20 -o filename <LINK>
# -a shows all progress during download
# -n 20 number of connections which is 20 here
# -o filename to save

9 - CUSTOMIZING THE BASH ENVIRONMENT

9.1 - BASH HISTORY CUSTOMIZATION

export HISTCONTROL=ignoredups # Remove duplicate variables
export HISTCONTROL=ignorespace # Remove commands start with space
# by default oth are enabled

export HISTIGNORE="&:ls:[bf]g:exit:history" # Ignore specific commands

export HISTTIMEFORMAT='%F %T ' # Show date and 24 hour format clock
# Other formats can be found in strftime man page

9.2 - ALIAS

alias lsa = 'ls -la' # When we enter lsa it will execute 'ls -la'

9.2 - PERSISTENT BASH CUSTOMIZATION

10 - WRAPPING UP