grep, cat, more, head, tail
Search for pattern & return lines matching the pattern
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?
Concatenate files & print results on the standard output
- 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
Page through text one screenful at a time
- spacebar = advance another screen
- b = back one screen
- q = quit
Output the first part of files
head -20 /etc/inittab
Output the last part of files. Especially useful for log files, which append newest entries at end of file.
tail -20 /etc/inittab
;, |, >, <
; (command terminator)
Terminates commands so that several can be run in sequence
cd; ls -F
| (pipe symbol)
Conduit between commands in which output from first command becomes input for second command
ls /dev | grep hda
ps -aux | grep root
> (greater than symbol)
Redirect stdout from monitor to a file
ls -F /etc > /tmp/etc.txt
cat /etc/passwd /etc/inittab > /tmp/longfile.txt
ps aux | grep root > /tmp/psroot.txt
< (lesser than symbol)
Redirect stdin from keyboard to a file, so that a command gets input from file instead of keyboard
sort < /etc/passwd
sort < /etc/passwd > /tmp/passwd.txt
The Wildcards: *, ?, [ ]
Match zero or more characters in a filename
cp /etc/* /tmp/etc
? (question mark)
Match any single character in a filename
cp photo?.jpg /mnt/floppy
[ ] (square brackets)
Match any single character between brackets in a filename
Show list of all commands entered on the command line
!!= repeat last command
↑ [up arrow]= back through history
Alternative name for a longer command
alias= display list of current aliases
alias la='ls -aF' ; alias
- Then try this: "la > /tmp/ls.txt"
- Redefine current commands so they have less to type
alias rm='rm -i'
- If you're paranoid & want to be reminded every time …
unalias la ; alias
&, ps, kill
Run processes in the background, so they don't take control of your terminal (& have to be killed with ctrl+c)
- See how my terminal is "locked" by 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 &
Display list of running processes
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
End running process
mozilla ; ps -aux ; kill [mozillaPID]
kill -9 PID= die now!
killev= kill all Evolution processes
Print curent date & time
rdate -p -s ntp.dgf.uchile.cl= connect to remote NTP server & set system time
cal 1 2002
cal 1 02
cal 9 1752
wc, sort, split
Display line, word, & character count of text file
cd tmp ; ln -s /usr/share/doc/HTML/en/common/gpl-license gpl
wc -l gpl= number of lines
wc -w gpl= number of words
wc -c gpl= number of characters
Sort lines in a text file to stdout
sort /etc/passwd > /tmp/passwd.txt
Split large file into smaller files
split -100 gpl splitgpl.
cat splitgpl.?? > gpl
Display amount of disk space available on the filesystem
df -h= human-readable (K, M, G)
Summarize disk usage for a file, or recursively for directories
cd /tmp ; du
du -c /etc= print total
du -ch /etc= human-readable