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
- Produces error: "grep: /etc: Is a directory"
- Why doesn't this work?
grep network /etc/*.conf
- Why does this one work?
grep network /*
- What am I trying to do?
- Produces error: "grep: /bin: Is a directory"
- Why doesn't this work? Notice also that it only looked at the immediate contents of /. It didn't go into any of those directories. Why not? What option should I have used?
grep -r network /*
- What does "-r" do?
cat
Purpose
Concatenate files & print results on the standard output
Examples
cat /etc/passwd
cat /etc/inittab
- Notice how the contents of the file zooms past
cat /etc/passwd /etc/inittab
- Note that you are NOT messing with the original files â•„ you're just concatenating them both & displaying the results to the screen
more
Purpose
Page through text one screenful at a time
Examples
more /etc/inittab
- spacebar = advance another screen
- b = back one screen
- q = quit
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
- Then try this: "la > /tmp/ls.txt"
alias md='mkdir'
- Redefine current commands so they have less to type
alias rm='rm -i'
- If you're paranoid & want to be reminded every time …
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
- See how my terminal is "locked" by Nessus?
nessus &
- My terminal is unlocked and free for other commands. The number in backets is the process ID number for 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
- Note that init is always process 1; hence it is the "mother of all processes"
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