Lab exercise #1
To become more familiar with the command line the following exercises are suggested.
You will need to be logged into a Linux system, or open CygWin on Windows.
When doing the exercises keep in mind that LINUX consists of two things:
FILES and PROCESSES - everything is a file, or a process...
stdin (standard in) is typically from a keyboard, files created by special device drivers
stdout (standard out) is typically displayed on a monitor, again, files - special drivers
But Linux uses IPC - inter-process communication to allow redirecting the
stdout from one process to the stdin of another allowing filtering and manipulation
of the data in files.
So... Once you have a shell open and you are in your home directory begin typing the following:
1) type: pwd
what was displayed? (pwd - print working directory, or present working directory)
2) type: cd .. ; ls
now you will likely see /home
3) type: cd ; ls
now you should see what was displayed in step 1
4) type: man pwd
(to exit press "q") this is the manual page for pwd, or the help documents
3) type: man pwd | col -b
it looks different... col -b is a filter to remove non-printable characters,
the pipe symbol "|" is the inter-process communication tool that directs stdout
to stdin allowing multiple processes to be executed at one time. This allows
data to be filtered repeatedly using specialized tools.
4) type: man pwd | col -b > man.pwd.txt
5) type: ls
6) type: ls -al
7) type: ls -Al
note the differences in the file listing (ls).
the ls -al command showed two lines with a dot, and then two dots.
the single dot represents the current directory
the two dots represent the parent directory. Remember, everything is represented by a file.
8) type: man ls | col -b | tee man.ls.txt
we've now used to pipe symbols, displayed the man page for ls, filtered non-printable
characters and used "tee" to direct the output to stdout AND to a text file.
9) type: more man.ls.txt
(to quit more, type q, to move down, use the space bar or enter (by line),
use b to go back up, if you hit "v" you will enter the vi editor - wait on that)
10) type: ls -alt
now you will see the file you created last at the top, the -t option is time
11) we've just covered the five essential commands: man, ls, cd, pwd and more
12) type: cd /etc ; ls -alR | more
you changed directory to /etc, then added another command, separated by the semi-colon
to do a long listing... RECURSIVE... this will list all the files below that point.
and, because it might be a long list, we piped it into more, allowing us to move up
and down using the space bar and B. No mouse was needed. shoot the rodent, he'll
just slow you down... (but only wing him... because he comes in handy for selecting
and copying text from the screen or web)
13) type: man more | col -b > man.more.txt ; man cd | col -b > man.cd.txt
14) type: man man 1 | col -b > man.man.txt
(man man 1 selects the first section of the man pages, there are 8 sections)
15) type: ls -Al
you should have editable, viewable help pages for pwd, ls, more, man and cd
16) below is a short script that uses a for loop that takes all the LPI commands found
in the certification test and creates a plain text manual page for review by
substituting "x" for the name listed in the variable "TOOL"
man $x | col -b > man.$x.txt
type the following:
type: vi createman.sh (you will type all the text starting from #!/bin/bash
until the final row of ### with end of script)
to start typing text:
(type the letter "i") (this begins the insert mode in vi)
(type exactly everything below) (if you make a mistake you
can delete a character from the command mode by typing x.
to enter the command mode, type and then the commands
to enter the insert mode, type "i". to commands, <i> to insert)
(hit the esc key, then :, then wq) :wq (command mode, write quit...)
type: sh ./createman.sh
type: ls -al
if you typed everything correctly you should have a list of files
that contain the man pages for commands listed below. If you have
files that are 0 bytes, that means it wasn't a command but a shell built in
and no man pages were available. next, we'll use a tool to remove blank files.
#!/bin/bash
##########################################################
## simple script with for loop
###########################################################
TOOL="dmesg bootloader ldd ldconfig dpkg dpkg-reconfigure apt-get
apt-cache aptitude rpm rpm2cpio yum yumdownloader bash echo env
exec export pwd set unset man uname history cat cut expand fmt
head od join nl paste pr sed sort split tail tr unexpand uniq
wc cp find mkdir mv ls rm rmdir touch tar cpio dd file gzip
gunzip bzip2 tee xargs bg fg jobs kill nohup ps top free uptime
killall nice ps renice top grep egrep fgrep sed regex vi fdisk
mkfs mkswap du df fsck e2fsck mke2fs debugfs dumpe2fs tune2fs
mount umount quota edquota repquota quotaon chmod umask chown
chgrp find locate updatedb whereis which type" ; export TOOL
#
for x in $TOOL
do
man $x | col -b > man.$x.txt
done
ls -al
############################################## end of script ####
16b) SAVE your work <esc>:wq
16c) execute script: sh ./createman.sh
17) type: ls -al | grep 0
what is displayed? every line that has a 0. some of the commands
may not have had man pages and would have been zero bytes
since YMMV (your mileage may vary), just showing ? as a command
that didn't have a man page...
-rw-r--r-- 1 user users 33566 Nov 25 00:35 man.man.txt
-rw-r--r-- 1 user users 0 Nov 25 00:35 man.?.txt
or... touch man.empty.txt (touched a file... created an empty file)
18) to find all files with zero bytes, or empty files type:
find . -type f -size 0 -ls -al {} \;
--> find . -type f -size 0 -exec ls -al {} \;
-rw-r--r-- 1 pt830a users 0 Nov 25 00:42 ./man.empty.txt
19) using filters like awk we can extract information and then sort it how we'd like:
--> ls -al
total 104
drwxr-xr-x 3 user users 4096 Nov 25 00:43 .
drwxr-xr-x 11 user users 4096 Nov 24 11:31 ..
-rw-r--r-- 1 user users 11121 Nov 25 00:43 exercise-practice.html
-rwxrwxr-x 1 user users 33291 Nov 24 23:47 linux-training.html
-rw-r--r-- 1 user users 0 Nov 25 00:42 man.empty.txt
-rw-r--r-- 1 user users 33566 Nov 25 00:35 man.man.txt
drwxr-xr-x 2 user users 4096 Nov 24 23:46 .pix
-rw-r--r-- 1 user users 5170 Nov 24 23:44 README.html
20) using awk as follows, produces:
--> ls -al | awk '{print "filename: "$9" is "$5" bytes" }'
filename: is bytes
filename: . is 4096 bytes
filename: .. is 4096 bytes
filename: exercise-practice.html is 12376 bytes
filename: linux-training.html is 33291 bytes
filename: man.empty.txt is 0 bytes
filename: man.man.txt is 33566 bytes
filename: .pix is 4096 bytes
filename: README.html is 5170 bytes
21) to eliminate directories:
--> ls -al | grep -v ^drwx | awk '{print "filename: "$9" is "$5" bytes" }'
filename: is bytes
filename: exercise-practice.html is 12102 bytes
filename: linux-training.html is 33291 bytes
filename: man.empty.txt is 0 bytes
filename: man.man.txt is 33566 bytes
filename: README.html is 5170 bytes
22) to get rid of the line without a file name or size:
--> ls -al | grep -v ^drwx | awk '{print "filename: "$9" is "$5" bytes" }' | grep -v "is bytes"
filename: exercise-practice.html is 12431 bytes
filename: linux-training.html is 33291 bytes
filename: man.empty.txt is 0 bytes
filename: man.man.txt is 33566 bytes
filename: README.html is 5170 bytes
23) to get rid of the 0 byte file, add another grep -v: (to continue a line for readability in a script you
may use "\", but it doesn't work at the command line, let the screen wrap.
--> ls -al | grep -v ^drwx | awk '{print "filename: "$9" is "$5" bytes" }' \
| grep -v "is bytes" | grep -v "txt is 0 bytes"
filename: exercise-practice.html is 12816 bytes
filename: linux-training.html is 33291 bytes
filename: man.man.txt is 33566 bytes
filename: README.html is 5170 bytes
24) we've covered the 5 basic commands, introduced find, awk, grep and the vi editor.
============================================================================================
john meister, lab manager, mstm
IT systems design and integration specialist / Linux, UNIX and lesser OSes
============================================================================================
|