Notes on the bash Shell for Teachers

grep, cat, more, head, tail

grep

Purpose

Search for pattern & return lines matching the pattern

Examples

grep [your last name] /etc/passwd

grep network /etc

grep network /etc/*.conf

grep network /*

grep -r network /*

cat

Purpose

Concatenate files & print results on the standard output

Examples

cat /etc/passwd

cat /etc/inittab

cat /etc/passwd /etc/inittab

more

Purpose

Page through text one screenful at a time

Examples

more /etc/inittab

head

Purpose

Output the first part of files

Examples

head /etc/inittab

head -20 /etc/inittab

tail

Purpose

Output the last part of files. Especially useful for log files, which append newest entries at end of file.

Examples

tail /etc/inittab

tail -20 /etc/inittab

;, |, >, <

; (command terminator)

Purpose

Terminates commands so that several can be run in sequence

Examples

cd; ls -F

| (pipe symbol)

Purpose

Conduit between commands in which output from first command becomes input for second command

Examples

ls /dev | grep hda

ps -aux | grep root

> (greater than symbol)

Purpose

Redirect stdout from monitor to a file

Examples

ls -F /etc > /tmp/etc.txt

cat /etc/passwd /etc/inittab > /tmp/longfile.txt

ps aux | grep root > /tmp/psroot.txt

< (lesser than symbol)

Purpose

Redirect stdin from keyboard to a file, so that a command gets input from file instead of keyboard

Examples

sort < /etc/passwd

sort < /etc/passwd > /tmp/passwd.txt

The Wildcards: *, ?, [ ]

* (asterisk)

Purpose

Match zero or more characters in a filename

Examples

ls *

ls /etc/*.conf

cp /etc/* /tmp/etc

? (question mark)

Purpose

Match any single character in a filename

Examples

ls /etc/rc?.d

cp photo?.jpg /mnt/floppy

[ ] (square brackets)

Purpose

Match any single character between brackets in a filename

Examples

ls rc[1-3].d

history

history

Purpose

Show list of all commands entered on the command line

Examples

history

history 10

!10

!!= repeat last command

↑ [up arrow]= back through history

alias, unalias

alias

Purpose

Alternative name for a longer command

Examples

alias= display list of current aliases

alias la='ls -aF' ; alias

alias md='mkdir'

alias rm='rm -i'

unalias

Purpose

Remove alias

Examples

unalias la ; alias

&, ps, kill

& (ampersand)

Purpose

Run processes in the background, so they don't take control of your terminal (& have to be killed with ctrl+c)

Examples

nessus

nessus &

locate network > network.txt &

ps

Purpose

Display list of running processes

Examples

ps= list all processes running at that moment

ps -x= list all process owned by you

ps -ax= list all process running on the machine

ps -aux= list all processes running on the machine & identify owner

ps -aux —cols 125= list all processes running on the machine & identify owner & print results to screen 125 characters wide

ps -aux | grep root

kill

Purpose

End running process

Examples

mozilla ; ps -aux ; kill [mozillaPID]

kill -9 PID= die now!

killev= kill all Evolution processes

date, cal

date

Purpose

Print curent date & time

Examples

date

rdate -p -s ntp.dgf.uchile.cl= connect to remote NTP server & set system time

cal

Purpose

Display calendar

Examples

cal

cal 1 2002

cal 1 02

cal 9 1752

cal 2002

wc, sort, split

wc

Purpose

Display line, word, & character count of text file

Examples

cd tmp ; ln -s /usr/share/doc/HTML/en/common/gpl-license gpl

wc gpl

wc -l gpl= number of lines

wc -w gpl= number of words

wc -c gpl= number of characters

sort

Purpose

Sort lines in a text file to stdout

Examples

sort /etc/passwd

sort /etc/passwd > /tmp/passwd.txt

split

Purpose

Split large file into smaller files

Examples

cd tmp

split -100 gpl splitgpl.

cat splitgpl.?? > gpl

df, du

df

Purpose

Display amount of disk space available on the filesystem

Examples

df

df -h= human-readable (K, M, G)

du

Purpose

Summarize disk usage for a file, or recursively for directories

Examples

cd /tmp ; du

du /etc

du -c /etc= print total

du -ch /etc= human-readable

WebSanity Top Secret