Command line fun

export (setting a variable)


export b=10.11.1.220

Instead of typing things over and over, you can just assign variables.

after assigning, you can just do a dollar sign $b


var="My Var"

echo $var

this assigning method only works during the current session. If you create another bash session, it won't work.


There are default environmental variables in Kali Linux. You can view them by typing: env


Bash History Tricks (use the commands you used previously!)

type: history

then type, for example, !262 to call the command with the desgnated number assigned.


$HISTSIZE

$HISTFILESIZE

these variables control command history and can be changed .


command history search with CTR+R!


saving output to a file

echo "test" > redirection_test.txt

if you output something to an existing file, the content will be replaced. (no undo!)

echo "this is added! " >> redirection_test.txt

> will add a sentence after and not overwrite.

Redirect from a file:

wc -m < redirection_test.text

(counts word count)


**STDERR (0, 1 or 2) **

**2 = standard error? **

If you don't want certain standard errors to show in the terminal, type:

**2>/dev/null inside the command. **

**You can also redirect errors to a file: 2>output.txt **


Piping - use more than one command in one line.


Test searching

Grep

i.e) ls -la /usr/bin | grep zip

sed

echo "I need to try hard" | sed 's/hard/harder/"

output > I need to try harder

replacing hard with harder.

Cut

cut -f (specifying a field)

cut -d (delimiter)

i.e) echo "I'm from Kasama, Ibaraki, Japan." | cut -f 2 -d ","

output > Ibaraki

awk

data extracting

awk -f (field seperater)

echo "hello::there::friend" | awk -F "::" '{print $1, $3}'

output > hello friend

**cut can only use one separator but awk is much more flexible. **


head command shows the first 10 lines of a file.

uniq -c (shows the number of occurances)

sort -urn

-u unique

-r descending order

-n numerical order


Editing Files from command line

1. Nano

ctr+K cut line

ctr+U paste

ctr+W search within the file

ctr+X exit

2. VI

Extremely fast


File Comparison

-COMM

compare two files

comm scan-a.txt scan-b.txt

-DIFF

much more complex and supports more output formats

diff -c scan-a.txt scan-b.txt

diff -u (unified format) cleaner and shorter

-vimdiff (highlights)

ctr+w

] + C - jumps to the next change

[ + C - to the previous one

D + O change from the other window to the current one

D + P opposite of the DO


Background processes

ping -c 400 localhost < ping_results.txt **& **

or run a command and suspend it with ctr+Z and then bg it


Jobs control

jobs command show what jobs are running in the current terminal session

fg %1 (foreground the session)


Process control with PS (system-wide)

ps -ef (e selects all processes and f is full-formatting)

ps -fC leafpad (finds leafpad process)

kill command kills processes (needs PID)

  • TTY- The name of the controlling terminal for the process.

  • TIME- The cumulative CPU time of the process, shown in minutes and seconds.

  • CMD- The name of the command that was used to start the process.

ps aux

a -- all users

u- user oriented format

x - shows processes that are running in the background


File and command monitoring

Tail command

sudo tail -f /var/log/apache2/access.log

(continuously updates the logs)

Watch command (runs commands every n second)

watch -n 5 w

---> w command lists logged in user


Downloading files

wget - downloads files using HTTP and FTP

wget -O report.pdf https://www.gorigorisensei.com/reoirts/pentest_reports.pdf

research more on this!


CURL

curl -o report.pdf http://www~~~


AXEL -a -n 20 -o

good for large downloads


Bash history customization

export HISTCONTROL=ignoredups

export HISTIGNORE="&:ls:[bf]g:exit:history:clear"

(setting these variables will ignore duplicate commands and specified frequently used/meaningless commands"

HISTTIMEFORMAT='%F %T ' - setting different time formatting for history


ALIAS - shorter commands that we can customize

alias lsa='ls -la'


customization of bash

by editing .bashrc file we can set persistent alias commands.

type: less .bashrc to see the file.


Exercises: finding files that have been updated in last 7days

find -type file -mtime -7

-mtime days

-mmin minutes

Use a combination of watch and ps to monitor the most CPU-intensive processes on your

Kali machine in a terminal window; launch different applications to see how the list changes

in real time.

watch -n 1 ps -aux --sort=-%cpu

Last updated