0% found this document useful (0 votes)
35 views4 pages

Lab5 5 2

This document provides instructions for using metacharacters like asterisk, question mark, and brackets with the ls command to list directory contents in UNIX. It describes how each metacharacter works and provides examples of using them to selectively list files and directories based on name patterns.

Uploaded by

Byron Floreano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views4 pages

Lab5 5 2

This document provides instructions for using metacharacters like asterisk, question mark, and brackets with the ls command to list directory contents in UNIX. It describes how each metacharacter works and provides examples of using them to selectively list files and directories based on name patterns.

Uploaded by

Byron Floreano
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Fundamentals of UNIX

Lab 5.5.2 – Directory Listings with Metacharacters


(Estimated time: 30 min.)

Objectives:
• Review some commonly used metacharacters
• Use the ls (list files) command with metacharacters
• Use the Asterisk (*) to substitute for zero or more characters
• Use the Question Mark (?) to substitute for a single character
• Use Square Brackets to substitute for a range of characters
• Use the semicolon to execute multiple commands on one command line

Background:
In this lab the student will work with various metacharacters and use them with the ls command to refine
the student’s directory listings. Metacharacters are keyboard characters with special meaning to the
shell. A general definition of a metacharacter is any keyboard character that is not alphanumeric.
Metacharacters are used with many UNIX commands to provide greater flexibility. Some of the
metacharacters used with UNIX are similar in function to those used with DOS. The asterisk (*) and the
question mark (?) are metacharacters which are also known as wildcards. The student will work with the
ls command and the following common metacharacters with this lab.

Metacharacter Name Function


~ Tilde Shortcut to home directory
* Asterisk Character substitution (also called splat)
? Question Mark Character substitution
[] Square Brackets Range definition

Tools / Preparation:
a) Before starting this lab, the student should review Chapter 5, Section 5, Identifying and Using
Metacharacters.
b) The student will need the following:
1. A login user ID, for example user2, and a password assigned by their instructor.
2. A computer running the UNIX operating system with CDE.
3. Networked computers in classroom.

Notes:

1-4 Fundamentals UNIX 2.0—-Lab 5.5.2 Copyright  2002, Cisco Systems, Inc.
Use the diagram of the sample Class File system directory tree to assist with this lab.

Step 1. Log in to CDE


The student should login with the user name and password assigned by the instructor in the CDE entry
box.

Step 2. Access the Command Line


Right click on the workspace backdrop and click on Tools. Select Terminal from the menu to open a
terminal window.

Step 3. Use the Basic ls Command


The ls (list files) command, when used by itself, will display a listing of all files and directories in the
current directory. If the student has just logged in, the student’s current directory should be the home
directory.

a. Enter the command to change to the student’s home directory. What command was used?

b. Enter the command to verify the directory the student is currently in. What command was used?

c. Enter the following command: $ ls What is displayed?

Step 4. Use the ls Command With the Asterisk (*) Metacharacter


The asterisk (*) is a substitution symbol that represents zero or more characters, except the leading dot
on a hidden file. The asterisk is often referred to as a wildcard character. If there were a large number of
files in a directory and a user only wanted to see a listing of project files that started with p1 the user
could use the asterisk to limit the numbers of files listed. As an example, the command ls p1* would list
all files and directories starting with p1 and any number of characters after that. The asterisk can be
placed anywhere, whether at the beginning, middle, or at the end of the string being tested. The asterisk
can also appear multiple times. If a user enters ls d* they will see only those files beginning with the
letter d and the contents of any subdirectories that start with the letter d.

2-4 Fundamentals UNIX 2.0—-Lab 5.5.2 Copyright  2002, Cisco Systems, Inc.
a. Enter the command to list files and directories in the student’s home directory that start with the
letter f. What command was used? What was listed?

b. Enter the command to list files and directories in the student’s home directory that start with the
letter d. What command was used? What was listed?

c. Enter the command to list files and directories in the student’s home directory that end with the
number 1. What command was used? What was listed?

d. Enter the command to list files and directories in the student’s home directory that have the
characters ‘ru’ anywhere in the file name. What command was used? What was
listed?

e. Enter the command to list files and directories in the coffees directory that start with the letter n
using a relative pathname. What command was used? What was listed?

Step 5. Use the ls Command With the Question Mark


The question mark (?) is a substitution character that matches any single character, except for the leading
dot on a hidden file. The question mark is also referred to as a wildcard character. The example below
shows the use of the of the ls command using the question mark in the fourth position. This indicates
that the file or directory name must start with dir but any character can be in the fourth position and the
file name cannot be more than four characters long.

$ ls dir?

a. Enter the command to list files and directories in the student’s home directory that start with the
letters file in the first four positions with anything in the fifth position but are not longer than five
characters. What command was used?

b. What was listed?

c. Enter the command to list files and directories in the student’s home directory that start with the
letter f in the first position with anything in the second and third positions and the characters e3 in
the last two positions that are not longer than five characters. What command was used?

d. What was listed?

Step 6. Use the ls Command With Square Brackets


Square brackets ([ ]) can be used to match a set or range of characters for a single character position in
the file or directory. The characters inside the brackets do not generally need to be in any order, for
example [abc] is the same as [cab]. However, if a user is looking for a range of characters, the characters
must be in proper order, for example [a–z] or [3–9]. If a user wants to search for all alphabetic characters,
whether lowercase or uppercase, use [A–z] for the pattern to match. A user can use alphabetic or
numeric characters for the search pattern.
The examples below uses square brackets along with the asterisk wildcard character. The first example
defines a range and will list all files and directories that start with the lower case letters b through f with
anything after that. The second example specifies that the first character must be either the letter a or f
and anything can be after that.

3-4 Fundamentals UNIX 2.0—-Lab 5.5.2 Copyright  2002, Cisco Systems, Inc.
$ ls [b-f]*
dante dir1 dir3 file1 file3 fruit practice
dante_1 dir2 dir4 file2 file4 fruit2

$ ls [af]*
file1 file2 file3 file4 fruit fruit2

a. Enter the command to list files and directories in the student’s home directory that start with the
letters f through p with anything in the remaining positions. What command was used?
What was listed?

b. Enter the command to list files and directories in the student’s home directory that start with any
characters but have the numbers 1 through 3 in the last character. What command was used?
What was listed?

c. Enter the command to list files and directories in student’s home directory that start with either d
or p and have any characters in the remaining positions. What command was used?
What was listed?

Step 7. Use the Semicolon to Separate Commands


The semicolon (;) enables a user to enter multiple commands on a single command line before pressing
enter. The semicolon is also referred to as the command separator. The example below shows two
examples using the semicolon to separate commands. In the first example, the clear command will
clear the screen, the cd command will return the user to their home directory and the ls command will
list files in that directory. The second example displays the current date and time, and then the calendar
for the current month.
$ clear;cd;ls
dante dir1 dir3 file1 file3 fruit practice
dante_1 dir2 dir4 file2 file4 fruit2

$ date;cal
Wed Feb 28 11:05:39 MDT 2001
February 2001
S M Tu W Th F S
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28

a. Enter a series of commands on one line to clear the screen, display the current working directory,
and then display a long listing of files in the student’s home directory. What series of commands
were entered?

Step 8. Close the Terminal Window and Logout


Double click on the dash button in the upper left corner of the screen, and then click the EXIT icon on the
front panel.

4-4 Fundamentals UNIX 2.0—-Lab 5.5.2 Copyright  2002, Cisco Systems, Inc.

You might also like