Lab Creating, Copying, Finding, and Using Wildcards
Lab Creating, Copying, Finding, and Using Wildcards
Objective
In this lab, students will learn how to use wildcards to simplify file creation, copying,
and searching in Linux. Wildcards are powerful tools for working with multiple files
or directories at once.
Equipment/Tools Needed
Computer with Linux installed (Debian, Ubuntu, or a derivative)
Terminal application
Content
1. Introduction to Wildcards
Wildcards are special characters used in commands to represent other
characters or groups of characters. The most commonly used wildcards in
Linux are:
o *: Matches any number of characters (including none).
Step-by-Step Guide
Step 1: Create Files and Directories for the Lab
Before performing the exercises, we need to create files and directories to use with
the wildcards.
1. Create directories and files
Run the following commands to create directories and files for the search
process:
mkdir myfiles
mkdir backup
touch myfiles/file1.txt
touch myfiles/file2.txt
touch myfiles/file3.txt
touch backup/file4.txt
touch backup/file5.txt
This will create:
o A directory named myfiles
2. Verify creation
To ensure the files and directories were created correctly, list the contents:
ls -R
This will display all files and directories recursively.
Step 2: Using Wildcards to List Files
Wildcards can help you list files matching a specific pattern.
1. Use * to list all files in a directory
To list all files in the myfiles directory:
ls myfiles/*
Expected output:
myfiles/file1.txt
myfiles/file2.txt
myfiles/file3.txt
2. Use ? to list files with a specific character in their name
To list files that have a name with exactly one character before .txt:
ls myfiles/file?.txt
Expected output:
myfiles/file1.txt
myfiles/file2.txt
myfiles/file3.txt
3. Use [] to list files with names that match any of the characters inside
the brackets
To list files that start with file followed by either a 1 or 2:
ls myfiles/file[1-2].txt
Expected output:
myfiles/file1.txt
myfiles/file2.txt
Step 3: Copy Files Using Wildcards
You can use wildcards to copy multiple files at once based on patterns.
1. Copy all files from myfiles to backup
To copy all files from myfiles to backup:
cp myfiles/* backup/
This will copy all files from myfiles to backup.
2. Copy files that match a specific pattern
To copy all files that start with file1 from myfiles to backup:
cp myfiles/file1* backup/
This will copy file1.txt to backup.
Step 4: Find Files Using Wildcards
The find command allows you to search for files using wildcards.
1. Find files in myfiles directory with names matching a pattern
To search for files that start with file and end with .txt:
find myfiles/ -name "file*.txt"
Expected output:
myfiles/file1.txt
myfiles/file2.txt
myfiles/file3.txt
2. Find files with a specific character in their name
To find files in myfiles that have a single character before .txt:
find myfiles/ -name "file?.txt"
Expected output:
myfiles/file1.txt
myfiles/file2.txt
myfiles/file3.txt
3. Find files in both myfiles and backup that match a pattern
To find all files with .txt in their name in both directories:
find . -name "*.txt"
Expected output:
./myfiles/file1.txt
./myfiles/file2.txt
./myfiles/file3.txt
./backup/file4.txt
./backup/file5.txt
Step 5: Practical Exercises
1. Use * to list all files in the myfiles directory.
2. Use ? to list files that match the pattern of one character before the
file extension.
3. Copy all files from myfiles to backup using a wildcard.
4. Find all files in backup that start with file and end with .txt.
5. Use [] to find files in myfiles that match specific characters.
Key Deliverables
A log of all commands used during the lab.
A list of files copied and found using wildcards.
A brief explanation (150-200 words) on the utility of wildcards in file
management.
Rubric
Correctly uses
Some incorrect Multiple errors
wildcards with Minor errors in
Command usage or or confusion in
appropriate wildcard syntax or
Accuracy options in command
options and usage.
commands. usage.
syntax.
Most searches
All searches and Some searches
and copy Incomplete
Search and copy operations or copy
operations are searches or
Copy are completed operations
completed, with copy
Completion with accurate incomplete or
minor missing operations.
results. with errors.
results.
Detailed and
Wildcard accurate Good explanation, Basic Poor or no
Understandin explanation of but may lack explanation of explanation
g wildcards with some detail. wildcards. provided.
examples.