0% found this document useful (0 votes)
43 views

Lab Exercises

This document provides instructions for a series of Linux lab exercises. It begins by describing the directory structure with folders like home, bin, etc. Exercise 1 involves basic commands like ls, cd, pwd to explore the directory structure and view file permissions. Exercise 2 involves connecting via SSH and running commands like echo, date, hostname, and touch to output text, view the date, get the hostname, and create a file. A table also compares different shell environments like ksh, bash, csh, and zsh.

Uploaded by

Neymar Jr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Lab Exercises

This document provides instructions for a series of Linux lab exercises. It begins by describing the directory structure with folders like home, bin, etc. Exercise 1 involves basic commands like ls, cd, pwd to explore the directory structure and view file permissions. Exercise 2 involves connecting via SSH and running commands like echo, date, hostname, and touch to output text, view the date, get the hostname, and create a file. A table also compares different shell environments like ksh, bash, csh, and zsh.

Uploaded by

Neymar Jr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

 

  UNIX/Linux  Lab Exercises


 

Exercise 1‐ Exploring your ubuntu space 
We are going to be using ubuntu for our
   Linux
  exercises.
  The  top  level  directory
    of isis
(root)   contains
    the  following
   
directories: 

 
Conceptually, you might envision it as a filing cabinet hierarchy with these as the major file folders (below). 

 
Home is where your home directory is located.  But to keep it from getting lost, home is nested in 
several sub‐directories.  These subdirectories are dependent upon your onyen.  The path from 
root to home looks like this for sfenton:  
 
 
 
   

Exercise 1
  
  Command  Action 
1  ls  Lists the files and directories in the current directory. You will not have the same files in 
your directory that I have in mine.  

 
2  cd public_html  Changes directories to your UNC web space  
3  ls  Lists the files and directories in your current directory 
4  cd   Returns you to your home directory.  You can also use cd ~ 
5  ls ‐a   lists all files  (‐a flag) 

    1 
    UNIX/Linux  Exercises 

Notice there are several files beginning with . which weren’t listed before. 
6  ls ‐l  Files listing in the long format; displays file name, permissions, number of hard links, owner, 
group, size and time. 
7  ls ‐al  Combine flags to get all of your files listed in the long format. 
8  cd public_html  Changes directories to your UNC web space  
9  cd ~;ls  Two commands combined into one:  
Returns you to your home directory and lists the directory contents. 
This command should work either with or without the spaces. 
10  cd lost   Change directories, but give a wrong pathname and see what happens  
11  pwd  Lists your present working directory (where you are now) 

12  cd ../  move up one directory 


13  ls  This listing may take a minute – it is listing all the directories on the server under the first letter 
of your first name 
14  pwd  present working directory 
15  cd ../;ls  note: there is a space between cd and ../, but not between / and; 
16  Use your Up and Down arrow keys to move through your command history. 
Up takes you back one command; Down moves your forward one command. 
 
17  cd ../ ;ls  Press your Up key, then, add the space between the / and the ;  The commands should 
still work.  Linux can be forgiving about some things. 
18  cd ../ ls  Try the same command without the semi‐colon – you will probably get a failure. 
19  cd /  This will take you to the ROOT level 
20  pwd 
There is no path to show       
21  ls  These are all of the root (or top) level directories. 
22  echo $SHELL  echo command echos back the input.  Here we are asking it to echo back the name of the shell.
Lists which shell you are using – see chart below.     
You should be in ksh.  If not, LET ME KNOW!!  
23  who  Displays a list of current system users ‐ this will be a list similar to the image below. You can 
see the person, when they connected and where they are connected from. All of the users 
below are connected from different unc departments 
Note one username for the next step 

 
24  finger  Enter the any username from your who query (from the list above, I could enter sadams)  
username  Displays information known about username ‐ image below tells us that sadams is Scott 
Adams, the SILS IT director and that he has been one since 12:34 on June 5, with 2 
minutes, 27 seconds of recent idle time  

 
24  cd  change back into your home directory 

    2 
    UNIX/Linux  Exercises 

Comparison of command shells  
We are using the ksh shell on isis. 

    ksh   bash (v3.1)    csh   tcsh    zsh   


           
Introduced  1982  1987  1978  1981 ?  1990 
Platform‐ Yes  Yes  Yes  Yes  Yes 
independent 
Default user  $  bash‐<version  %  >  <hostname>% 
prompt  number>$ 
Tab completion  Yes (extendable)  Yes (extendable)  Yes (via the ESC  Yes (extendable)  Yes (extendable) 
key) 
Spell checking  No  No  No  Yes  Yes 
Search and  Yes (via ${//}  Yes (via ${//}  No  Yes (via ${:s//}  Yes (via ${:s//} 
replace on  syntax and builtin  syntax)  syntax  and ${//} syntax) 
variables  commands) 
Pattern  Yes  Yes  No  No  Yes 
Matching 
(regular 
expressions) 
Command  Yes  Yes  Yes  Yes  Yes 
history 
 
Troubleshooting: Getting unexpected results?     Check your syntax: 

1. Case of your command. Unix / Linux is case sensitive.  
2. Spaces (typically:)  
One space after the command and before the flags  
One space before the file or directory that is receiving the command  
No spaces between the flags 
No space around the ; joining two commands  
3. Order (typically:) example: ls ‐l /public html 
command      flags     acted‐upon‐thing(file or directory) 

Exercise 2 
Connect to ubuntu using
  SSH  secure
  shell  or other
    SSH  program.
  Enter
   these
  commands
  at the
   UNIX  prompt:
  type   
the command in and hit enter.  Do not type anything in the ( ).  Examine the results and try to interpret the output.  
 
Part  1 –more Linux basics      Be sure that you are in your home directory before you begin.! 
 
  Command  Action 
1  echo hello world   echo outputs  status text to the screen (or a file). 
2  date  Get today's date 
3  hostname   Hostname is the unique name by which a is known on a network. 
4  touch empty.txt  Create  a new empty file called empty.txt 

Fenton    3 
inls 461  UNIX/Linux  Exercises 

5  ls  Get a listing of all the files in the directory.  Is the file there? 
6  cat  empty.txt  cat allows you to look at, modify or combine a file..  Is there anything in the file? 
7  mkdir junk  Make a new directory called junk 
8  ls   
9  mv empty.txt junk    Move empty.txt into the junk directory. 
10  ls   
11  cd junk  Change directories into the junk directory 
12  ls  Is the empty.txt file in the junk directory? 
13  cd  Go back to your home directory. 
14  cp –r junk junk2  Make a copy of the junk directory and call it junk2.   –r is a recursive flag 
15  cd junk2   
16  ls  is empty.txt inside junk2? 
17  cd  ../  Move back up one directory (into home) . 
18  cp  –r  junk junk3  Recursively copy junk (and all of its contents) into junk3. 
19  cp   –r  junk  junk4  Recursively copy junk (and all of its contents) into junk4. 
20  ls  You should now have 4 junk directories 
21  touch file2.txt  Create  a new empty file called file2.txt 
22  ls  You now have several new files and directories.  Let’s get rid of them. 
23  rm  file2.txt  rm removes a file 
24  rm  junk4  Does rm remove the junk4 directory?  Why not?  ((directory is not empty) 
25  You have several options here:  
1‐ you can cd into the directory junk and remove everything then leave the directory and remove it. 
2‐ you can use rm with a recursive flag. 
26  rm –r junk4  ‐r flag removes the directory and all of its contents  
27  ls  is the directory junk4 gone? 
28  rm –r junk*  * is a wildcard.  junk* refers to any file that begins with junk.   
This will remove junk, junk2 and junk3 all at once. 
Use the * with caution 
29  ls  all junk directories should be gone. 
30  clear  This clears your screen.  It’s a great command if you are bothered by mess. 
 
Part 2­ Redirect, Pipe and Sort 
Redirect > or <

> redirects output


Redirects  standard output to go to a file. Redirect creates a new file or erases the existing file and replaces it 
with the current content.  This is useful if you want to save the output of a command to a file rather than just 
letting it scroll by so fast that you can't read it.  
< redirects input
 In an odd inverse logic, you can redirect an input file by using the < sign. This is used less frequently, and we will 
not cover it this course.  
>>   redirects output and appends  
Redirects the output and appends the information to an existing file 
 

    4 
    UNIX/Linux  Exercises 

Pipe |
• Pipes allow separate processes to communicate without having been designed explicitly to work together.  
• The symbol for pipe is |  This is located on the right side of the keyboard above the \ key  
 
Pipe and Redirecting Input and Output Exercise     
 
  Command  Action 
1  date  gives you today’s date 
2  date > today.txt   Redirects output of date to a file called today.txt  
(Note: unix does not require file extensions. you could just call the file today)  
3  ls  You should see the new file today.txt in your directory  
4  cat today.txt   This displays the contents of today.txt ‐ image below shows the past 3 
commands  
5  date > today.txt   Run the command again (use your arrow keys). 
6  cat date.txt   It will have overwritten the first date with a new date.  
Notice the time here is later than the time above 
7  date >> today.txt   If you want to add to a file instead of erasing it, use the >> command. This 
  appends new information to the end of the existing file. 
8  cat date.txt  Use your up arrow to move back through the commands.  You should have  2 
dates in your today.txt file 
9  who  Displays who is on the system. 
10  who > whoexercise.txt   This will redirect the output of the who command into a file called 
  whoexercise.txt 
11  ls   
12  cat whoexercise.txt  Display file contents. 
 
13  who | sort  This is a piping of the output of the who command to the sort command.  
This is not saved in a file, but just displayed on a screen.  
The sorting is done alphabetically on the first column. 
14  who | sort >> whoexercise.txt  This takes the information and appends it to the end of the existing 
whoexercise.txt file. 
15  ls   
16  For this next part of the exercise, we need a large directory listing, so we will use the /usr/bin directory 
17  ls ‐l /usr/bin  This asks for the long listing (‐l) of the files of the bin directory. When you run this, 
they will scroll by quickly  
18  ls ‐l /usr/bin | more   more displays text one screen at a time.  
19  touch redirect.txt  Create an empty file called redirect.txt 
20  ls   
21  ls ‐l redirect.txt  Get the long information on just this one file. 
22  ls ‐l /usr/bin  > redirect.txt  Redirect the output of ls –l to redirect.txt 
23  ls ‐l redirect.txt  Notice the time and size change. 
24  cat > list1  Output of cat will go to a file called list1 
25  peas  Type in names of foods. 
beans   Hit enter after each one. 
carrots 
bread 
cheese 
26  ^d  This stops the cat process.  Your file is created and the command prompt returns. 
27  ls   
28  cat list1.txt  Since there is now a file called list1.txt, the cat command will display the file 

    5 
    UNIX/Linux  Exercises 

contents. 
29  cat >> list1.txt  >>  redirects and appends.  Used with cat, this will allow us to append more text 
onto our existing list 
30  chocolate  Type in the name of more foods. 
cake   
cookies 
ice cream 
31  ^d  Press ctrl d to stop. 
32  cat list1.txt  Your list will now have both sets of foods in it. 
33  cat > list2.txt    This will begin a new file called list2.txt 
34  milk  Type in the name of beverages. 
coffee 
tea 
oj 
soda  
35  cat list2.txt  list2.txt is just a list of beverages 
36  cat list1.txt list2.txt >  Used this way, cat and > combine the 2 existing lists into a new file called 
biglist.txt  biglist.txt 
37  ls   
38  cat biglist  You should have all of your lists in one now. 
39  ls  We are going to delete our lists.  Check to see that you do not have anything with 
the string ‘list’ before using this next command.   
40  rm *list*  This will delete and ‘list’ files such as listerine.dat or evangelist.txt or 
enlistment.xls 

 
Exercise 3 – Sort, find and wc 
Sort   Sort   <flags> <sort fields> <file name>
The sort program arranges lines of text alphabetically or numerically.  
Sort doesn't modify the file itself; it reads the file and writes the sorted text to the standard output. 
Flag  Action 
‐n   Sort numerically (example: 10 will sort after 2), ignore blanks and tabs. 
‐r   Reverse the order of sort. 
‐f   Sort upper‐ and lowercase together. 
‐k  x sort by x column 

Pipe
Take the output of one program and send it to another as the input. With pipes, you can "glue" multiple commands 
together in a powerful way. 
The tar command is used to convert a group of files into a single file archive.  Created back in the days of magnetic 
tape backups, tar is an abbreviation for tape archive.  Tarball is the slang for a tarred archive of files. 
 
Part 1 – sort; retrieving (wget)and unpacking files (tar, gunzip)
  Command  page or comment 
1  sort      Type sort and press enter; this will begin the sort program.    
2  9  Type in the numbers. 

    6 
    UNIX/Linux  Exercises 





3  ^d  This ends the sort program.  Since we didn’t specify that the output should go to a 
file, the sort process show up on our screen.  (numbers are now sorted from 1‐9) 
4  sort > sorted.txt  Use > redirect to save the sort results in a file called sorted.txt 
  cat sorted.txt  Note that what you typed in is not saved, just the results of sort. 
5  who  see who else is on the server 
6  who > names.txt  Redirect the output of who to names.txt 
7  cat names.txt   
8  sort < names.txt  Redirect the input of names.txt to sort 
9  cat names.txt  Was the original names.txt file changed 
10  who | sort  produces same effect as steps 9‐11, with no leftover files 
  wget is a utility for non‐interactive download of files from the Web.  We will use it to retrieve data files. 
11  wget   http://www.unc.edu/~sfenton/data/entree.tar.gz 
12  ls  Check that you hav  entree.tar.gz 
13  gunzip entree.tar.gz  gunzip is an unzipping utility  
14  ls  file should now be entree_data.tar (it will have lost the .gz extension) 
15  tar –xvf entree_data.tar  untar the file.  Be sure to use the flags ‐xvf 
16  ls  file should now be a directory called entree 
17  cd entree  change to the entrée directory 
18  ls  on file (README) and 2 directories  (data/     &  session/) 
19  more README  read the README file to understand what this set is 
20  cd data  change to the data directory 
21  ls   
22  more README  remember to use up arrows to go back through your commands (saves time) 
23  more boston.txt  Read a few screens of the boston.txt file.   
Note the organization of the data (sorted by restaurant id) 
24  sort boston.txt  sort the restaurants alphabetically 
25  Did your sort work?  No – it sorts by the first column, which is numeric.  You need to sort by the second column. 
26  sort –k 2 boston.txt   
27  sort –kr 2 boston.txt  If you add the  r flag, will this sort in reverse order?  Why not? 
28  sort –rk 2 boston.txt  Why does this work?  Note the number at the very bottom of the list. 
29  sort –k 2 –r boston.txt  Why does this work?   
30  Do NOT delete the entrée directory.  We will be using it more!! 
 
Part 2 ­  find and wc 
 
find 
Searches the entire file system for one or more files.  Find can also then perform some type of action on the files after 
they've been located.  
find / ‐name *txt'   
/     Start searching from the root directory (i.e / directory) 
‐name     Given search text is the filename rather than any other attribute of a file 
'program.c'    Search text that we have entered.  Always enclose the filename in single quotes 

wc 

    7 
    UNIX/Linux  Exercises 

wc stands for "word count".  
The command returns line,  word, and byte (or character) counts for each FILE,  
and a total line if more than one FILE is specified.  
 #   wc films.dat 
 #    100     607    7329 films.dat 
Option  Action 
‐l   counts the number of lines in a file 
‐L  counts the length of longest line 
‐w  counts the number of words. 
‐c  counts the number of bytes 
‐m  counts the number of characters. 
 
Wildcards 
Here's a list of the most commonly used wildcards in bash or ksh.  You can use wildcards with any command that accepts 
file names as arguments.  In some circumstances, the wildcard string may require the use of double or single quotes.  

 
Wildcard  Matches 
  *  zero or more characters 
[abcde]  Range of characters 
 
Could be numeric [0‐4] or [1‐9] or letters [a‐g] or  [A‐G].  
  [a‐e] 
  Note : Linux is case sensitive; a range of [a‐z] is different from [A‐Z] 
  [!abcde]  any character that is not listed 
  [!a‐e]  any character that is not in the given range 
 
{debian,linux}  exactly one entire word in the options given 
 
 
Part 2­ Exercise – find and wc 
  Command  Comment 
1  ls  Confirm that you have the entrée directory from part 1 of this exercise 
2  cd  entrée/data  Change into the entrée directory 
3  ls  List the contents of the current directory 
4  find   Lists the contents of all current directory and all the subdirectories 
./ indicates your current directory. 
5  find . *.txt  Find is more explicit than; it gives more information 
6  cd ../session  Move up one level, into the session directory 
  ls  Look at what files are in this directory 
7  find *Q1  This is a wildcard search that looks for files ending in Q1 
8  find *Q  Another wildcard search.  Why doesn’t this one work?  Are there any files 
ending in Q? 
9  find   *“Q1”  This is a wildcard search that looks for files ending in Q1 
10  find   “*Q1”  This is the same search, but with the * within the   “  “ .  Why doesn’t it work? 
11  ls  ‐l *Q*  This list all the files containing *Q* (Note that  README is not listed) 
12  ls  *Q*| wc   List the files and pipe it to wc  
13  cd   ../  Move up one directory. 
14  pwd  Check your location. You should be in the entrée directory. 
15  find *1996*  Without flags, find will only search your current directory. 
16  find –name *1996*  The flag –name  will make find search down through the nested directories 
(sessions and data).   

    8 
    UNIX/Linux  Exercises 

17  find *.txt  Without flags or a path, find will only search your current directory. 


18  find . *.txt  . (as a path) means the present directory. This will search through the nested 
directories for any files ending in .txt  You will see the files listed, but results 
conclude: “find: *.txt: No such file or directory”  Why are we not finding our 
.txt files? 
19  find  –name  *.txt   ‐name is a flag to search by name.  This will search through the nested 
directories for any files ending in .txt 
20  echo  *  echo command echos back what you type after echo.  Here we are asking it to 
echo everything in the current directory. 
21  echo  /*  Prints contents of / directory 
22  wget http://www.unc.edu/~sfenton/data/poetry.tar.gz 
23  gunzip   poetry.tar.gz   
24  tar  –xvf   poetry.tar  untar the file.  Be sure to use the flags ‐xvf 
25  ls  Confirm that you have a directory called poems 
26  echo  poems/*  This will show you the contents of poems.   
The syntax poems/* says all files (*) within the directory (/) poems 
27  cd poems   
28  ls –l   Notice that there are several text files and three directories within poems. 
29  cat beo*  Use cat command with the * wildcard to read the beowulf.txt file 
30  Ctrl C  Ctrl C to stop the process. 
31  cat beo* | sort  Read the Beowulf.txt file and pipe it to sort.  This will create an alphabetical 
version of Beowulf.  
32  sort beo*  This does the same thing 
33  cd  Return to your home directory 
34  find    Displays all files in the current directory and all subdirectories.  
35  find  poe*  You should get no results – what’s missing? 
36  find  . poe*   Add the path and you will get listings of everything searched, but the results 
state: “find: poe*: No such file or directory” 
37  find  . ‐name "poe*"  Use “ ” with wildcards – what changed with the “  “ ? 
38  find / ‐name "poe*"  / says to begin searching in the root directory.  Use ctrl C to quit 
39  find  /etc/  ‐name "*.txt"  This may take awhile.  Where are you searching and for what? 
40  find /etc/ ‐mtime ‐6  Modified by time flag – find files modified in the past 6 days 
41  wc  ‐‐help  You can use help instead of the man pages. 
42  cd  poems  Move into the poems directory 
43  wc   poe.txt  Three way to count lines, word, bytes in a file: 
1.  counting the poe.txt file  
44  wc  <  poe.txt  2.  counting the poe.txt file 
45  cat  poe.txt  |  wc  3.  counting the poe.txt file 
46  wc  –c  poe.txt   Count by characters 
47  wc  –w  poe.txt  Count by words 
48  wc –l poe.txt  Count by lines 
49  echo  *.txt  *   =  zero or more characters 
50  echo  l*.txt  This is asking for files that begin with l lowercase (L) 
51  echo  *l.txt  This is asking for files that end with l 
52  echo  *l*.txt  *   =  zero or more characters  
53  ec ho  *ll*.txt  *   =  zero or more characters – What happened to Eliot? 
54  echo *[ll]*.txt  [  ]  = any of the characters in this range.  This  will return l or ll  
55  echo  c*  *   =  zero or more characters 
56  echo  c[au]*  [  ]  = range of characters  ‐ Why is Carroll returned and not Chaucer? 

    9 
    UNIX/Linux  Exercises 

57  cd  Return to your home directory 


 

Exercise 4 ‐ grep 
 
The name "grep" derives a command in an editing program:  g/re/p which means "globally search for a regular 
expression and print all lines containing it. 
 
syntax:     
 grep  "pattern"   file(s)  
 
The simplest use of grep is to look for a pattern consisting of a single word. 
You can pipe |  the output of grep to the input of another command 
You can redirect  > the output of grep into a file 
 
This section will use the termcap file, which is found in the etc directory within the root directory.  Termcap is a 
multipage file with specifications about all of the terminals supported on the Linux system.   
The syntax for this will be:   grep  ‐pattern /etc/termcap 
 
Flag  Action 
‐i  Match either upper‐ or lowercase. 
‐n   Matched line and its line number. 
‐l   Names of files with matching lines (letter "l"). 
‐c   Count of matching lines (not individual occurrences!). 
‐v  Lines that do not match pattern. 
‐w    Match whole words 
 
 
Grep exercise 

Using SSH, login to isis.  Choose option 6 to get a shell window. You will be in your home directory.  Type ls to see the 
contents your current directory.  Follow the commands listed below. 
You will need the entrée dataset:      http://www.ics.uci.edu/~kdd/databases/entree/entree_data.tar.gz 
Download it using wget; run gunzip and tar to unpack the dataset. 
  Command  Page or comment 
1  cat  /etc/termcap  cat sends text to standard output; ctrl c to quit 
This will let you view the contents of the termcap  file in the /etc directory. 
2  more  /etc/termcap  scroll through by using enter key or spacebar; 
Type q to quit. 
3  less  /etc/termcap  less is similar to more, but with more options. 

Use down   and up   arrow keys to move down or up. 


To go down one screen: space, z or f 
To go up one screen, use: b    To go up one line, use:  y   Type q to quit. 
4  head    /etc/termcap  View the first 10 lines of the file 
5  tail    /etc/termcap  View the last 10 lines of the file 
6  whatis   tail  Display a brief description of the command cat 

    10 
UNIX/Linux  Exercises 

7  whatis   grep  Display a brief description of the command grep 
  man grep  Skim the man page for grep 
8  grep   iris   /etc/termcap  Search for word iris 
9  grep   iris /etc/termcap  | wc  Search for word iris and send iris results to wordcount 
10  grep Iris /etc/termcap  | wc  same, but using uppercase Iris 
11  grep –i  iris /etc/termcap |wc  case insensitive (iris or Iris) 
12  grep ‐ic  iris /etc/termcap   case insensitive using iris count flag (counts  lines only) 
13  grep ‐i  Aha /etc/termcap  Search for the word aha (case insensitive) 
14  grep ‐in  Aha /etc/termcap  same, but include the line numbers 
15  cd   change to your home directory (you should have been there already) 
16  grep   Lee   entree/data/*  search for Lee in all the files in the data directory 
17  grep  ‐l   Lee  entree/data/*  This give you file names ONLY. 
18  grep ‐i Lee entree/data/*  case insensitive search for Lee in all the files in the data directory 
19  grep ‐ic Lee entree/data/*  case insensitive; counts the number of lines in each file. 
 
Wildcards and Regular Expressions 
If you are having problems, try putting the pattern with the wildcard inside single or double quotes. This protects the 
wildcard from expansion by the shell, before grep gets the pattern. 

abc  Match lines containing the string "abc" anywhere. 
$  end of the line    abc$ 
^  beginning of the line     ^abc 
.  exactly one character 
a..c  a  and  c  separated by any two characters (the dot matches any single character). 
*  The preceding item will be matched zero or more times. 
a  and  c    separated by any number of characters  
a.*c 
(the dot‐ asterisk combination means match zero or more characters) 
 [    ]  To match a selection of characters 
[0‐3]  is the same as [0123] 
[a‐k]  is the same as [abcdefghijk] 
[A‐C]  is the same as [ABC] 
[A‐Ca‐k]  is the same as [ABCabcdefghijk] 
\  turn off the special meaning of the next character, as in \^ 
\|  or operator between 2 search terms.  Search string must be  enclosed in [  ] or “  “   
\<  To match the start of a word 
\>  To match the end of a word 
• e.g. search only for words ending in hello 
• grep 'hello\>' * 
• This will match `Othello' 
 
Wildcard Exercise  
  Command  Action 

1  ls ‐l /usr/bin  long listing of the directory /usr/bin 

2  ls   /usr/bin  short listing of /usr/bin 

3  ls   /usr/bin | grep  yp  grep the short  listings of usr/bin. 
Output is piped to grep command, which searches for the text 
string head 

    11 
UNIX/Linux  Exercises 

4  ls ‐l /usr/bin | grep  yp  grep the directory listings of usr/bin. 
Output is piped to grep command, which searches for the text 
string head  
5  ls   /usr/bin | grep  yp  | wc  short version – piped to wc 

6  ls ‐l /usr/bin | grep  yp  | wc  long version – piped to wc 
Why are there different counts  between 5 and 6? 
7  ls ‐l /usr/bin | grep ‐i yp  | wc  Add the –i  flag to make grep case insensitive .  Why fewer results? 
8  ls ‐l /usr/bin | grep ‐ic yp    Another way to make the query case insensitive and count it. 

  ls ‐l /usr/bin | grep yp | sort  sort numerically by the first column  
Not really too useful. 
  ls ‐l /usr/bin | grep  yp | sort –k  6  sort by the 6th  column  to sort by date  

  ls ‐l /usr/bin | grep  yp | sort ‐k  6 ‐r  files size, sorted in reverse order 

  Using wget retrieve the Project Gutenberg text file of Fenimore Cooper's Literary Offences by Mark Twain   
http://www.unc.edu/~sfenton/data/twain.txt 
  ls  Confirm that you have your file. 

  grep disappointment twain.txt   Using grep without any flags gives us a listing of the lines 
containing the words "disappointment" 
  grep ‐c disappointment twain.txt   Adding the ‐c flag give us the count of the lines where 
disappointment occurs  
  grep ‐n disappointment twain.txt   Using the ‐n flag give us the line number (location within the file) 
of occurrences of disappointment, as well as the actual line 
themselves  
  grep "disappointment\|agony" twain.txt  this will search for either disappointment or agony 

  Let’s assume that you want to search for all the places that the pronouns he or she occur in twain.txt.  Start 
with he, 
  grep he twain.txt   Look at the results.  Are these all the pronouns he? Or other 
occurrences of the string he? 
  grep ‐c he twain.txt  Get a count of the occurrences of he.  

  grep  "he" twain.txt  If we put he in “ “ will that work? 

  grep ” he “ twain.txt  You need to include the spaces both before and after to search for 
he as a word 
  grep ‐c " he " twain.txt  count the occurrences. 

  grep –w he twain.txt  Use the flag –w  to force a search for whole words 

  grep –w  he twain.txt |wc  Count the results with the –w flag.  Look at  your results and see if 
you can figure out why using the –w flag gives you more results. 
  grep –w he twain.txt  Look at the file and see what you can observe. Look carefully at 
punctuation.  “  he  ” means that there will be a space before and 
after he. 
  grep ‐ic him twain.txt  Count of the case insensitive search for him  

  grep ‐iwc him twain.txt  Count of the case insensitive search for the word  him.  Notice the 
difference in searching for the string him or the word him. 
  grep ‐c  [Hh]im  twain.txt  Count of case insensitive him. 

  grep  ‐wc  [Hh]im twain.txt  Count of the case insensitive search for the word  him 

    12 
UNIX/Linux  Exercises 

  clear  clear your screen 

 
This next set of exercises is using the entrée dataset, which you should have downloaded and unzipped already 
  ls  Confirm that you have the entrée file downloaded and unzipped 

1  cd  entree/data  If you are in /poems, you will need to cd ../entrée/data 

2  ls  you should see files like: atlanta.txt, boston.txt, etc  

3  You are going to search on variations of the word cafe 

4  grep  cafe atlanta.txt  Find instances of the string cafe  

5  grep ‐c  cafe atlanta.txt  A count of matching lines.  Answer should be 0 

6  grep  Cafe atlanta.txt  Find instances of the string Cafe  (uppercase) 

7  grep ‐c  Cafe atlanta.txt  A count of matching lines.  Answer should be 18 

8  grep ‐ic  cafe atlanta.txt  Case insensitive; A count of matching lines.  Answer should be 19 

9  grep ‐i  cafe atlanta.txt  List the returns; case insensitive.  What is unusual in the handling 
of the string café? 
10  grep [cC]afe atlanta.txt  another way to get a list with both upper and lower case 

11  grep –c [cC]afe atlanta.txt  A count of matching lines.  Answer should be 18 

12  grep Cafe *.txt  *.txt searches all of the text files in the current directory.  


Returned results list the directory 
13  grep –ic cafe  *.txt   case insensitive and counted 

14  grep –ic    c.fe    *.txt   .  = substitute any one letter  

15  grep    00002.2   atlanta.txt  Does . work as a substitute for a number? 


16  grep ‐c   00002.2  atlanta.txt  Count the returned results. You should get 7. 

17  grep    00002*2   atlanta.txt  Can you use * to get the same results? 

18  grep  ‐c 00002*2 atlanta.txt  You should get 78. 

19  grep  00002[0‐9]2 atlanta.txt  Does this syntax work? 

20  grep ‐c  00002[0‐9]2 atlanta.txt  You should get 7. 

21  variations on the search for ‘to’ 

22  grep .to. README  Does .to. give you the word to? 


What exactly are you searching with . ? 
23  grep *to* README  Does .to. give you the word to? 
What exactly are you searching with * ? 
24  grep " to " README  Use spaces to define the string.  Does this give you the word to? 

25  grep ‐w  to README  ‐w forces the pattern to match only whole words 

26  grep ‐w the README  look at the distribution of ‘the’ in the retrieved lines 

27  The next group are searches to find a name that ends in ‘s.  ________’s  in Atlanta 

    13 
UNIX/Linux  Exercises 

28  grep   \'s atlanta.txt  turn off the special meaning of the next character (the apostrophe) 

29  grep ‐c \'s atlanta.txt  A count of the occurrences (90 occurrences). 

30  search for anything ending in y’s   (Danny’s, Tony’s, etc) 

31  grep   y\'s atlanta.txt  case insensitive, match anything ending in y’s  (Danny’s, Anthony’s) 

32  grep  ‐c  y\'s atlanta.txt  15 occurrences. 

33  grep  *y\'s atlanta.txt  No results?  What does * mean in this use? 

34  grep  .*y\'s atlanta.txt  The dot‐ asterisk    .*   combination means match zero or more 


characters. 
35  grep  ‐c  .*y\'s atlanta.txt  15 occurrences. 

36  grep  "[a‐z]y"\'s atlanta.txt  same, using square brackets 

37  grep  c ".y"\'s atlanta.txt  15 occurrences. 

38  grep  “grill\|café” atlanta.txt  \|  works as an or 

39  grep [\$] *.txt  \ allows a search for a $ in any of the text files 

40  cd ../  You are now in the entrée directory 

 
Summary: 
Try a variety of flags and regular expressions. 
Try alternate flags and expressions to check yourself.  Are you really finding what you think? 
Read the man pages! 
 

Exercise 5 
Using Pico text editor  
  Command  Action 
1  ls  list directory contents  
2  mkdir newdirectory   create a new directory called newdirectory  
3  cd newdirectory   change directories into the newdirectory directory  
4  ls  list directory contents ( there shouldn't be any. This is a new, empty 
directory)  
5  pico  Start the pico file editor  
6   (type the rhyme below and be sure to include some misspellings! ) 
Jock and Bill
Went up the hill
To fetch a snail of water.
Jack fell down
And broke his crown
And Jill came tumbling after.

Up Jack got
And home did trot
As fast as he could caper

    14 
    UNIX/Linux  Exercises 

Went to bed
And plastered his head
With vinegar and brown paper.
7  Move back up and correct the words Jock, Bill  
8   ctrl 0  Save without exiting (save file as pico_exercise.txt ) Pico will ask you 
if you would like to "save modified buffer? ‐ Type y  
9  Move down and correct the word snail  
10   ctrl X  Save and exit ‐ Pico will ask you if you would like to "save modified 
buffer? ‐ Type y 
11  pico pico_exercise.txt  Open the file to check it:  
12  ctrl V  Move down ‐ repeat until you are at the last paragraph  
13  ctrl Y  Move up ‐ until you are at the top of the document  
14  ctrl W  Allows search for a word. Enter the word: piano and hit return. 
This will take you to the word piano.  
If you type ctrl W again, the last word searched (piano) will be 
offered as a default search. 
Try a repeated search of the word field ‐ you should find three 
occurrences of field.  
15  ctrl X  Exit, but do not save changes (type n to 'save modified buffer')  
16  cat pico_exercise.txt more  This will quickly scroll through the entire file ‐ probably too quickly for 
pico_exercise.txt  you to read it  
This will scroll through the file slowly  
Press the enter key to move on to the next screen one line at a time. 
Press the space bar to move through one screen at a time. 
shift Q will end the more command.  
17  exit  Ends the shell session and returns you to the isis login prompts  
18  ctrl D  Ends the terminal session. Close the SSH window!  
 

Note you can copy text and paste text  into pico using the key combination:    shift     insert 

    15 

You might also like