Introduction
Linux wildcards are special characters that allow you to search and manipulate multiple files at once based on patterns in their names. They are powerful tools for efficiently managing files in the command line environment. In this lab, you will learn how to use different wildcards to perform common operations such as listing, copying, and removing files based on pattern matching.
Understanding the Asterisk (*) Wildcard
The asterisk * is the most common wildcard character in Linux. It represents any number of characters (including zero characters) in a file name. This makes it incredibly useful for working with groups of files that share similar naming patterns.
Let's practice using the asterisk wildcard:
First, open your terminal and navigate to the project directory:
cd ~/projectCreate a practice directory for this lab and navigate into it:
mkdir -p wildcard_practice cd wildcard_practiceNow, let's create some sample files to work with:
touch file1.txt file2.txt file3.txt document1.pdf document2.pdf image.jpgUse the
lscommand to list all files in the directory:lsYou should see all the files you just created:
document1.pdf document2.pdf file1.txt file2.txt file3.txt image.jpgNow, let's use the asterisk wildcard to list only the text files:
ls *.txtThis command should display only the files ending with
.txt:file1.txt file2.txt file3.txtYou can also use the wildcard to match files that start with a specific pattern. To list all files that start with "file":
ls file*This command should show:
file1.txt file2.txt file3.txtTo list all PDF documents:
ls *.pdfThis command should display:
document1.pdf document2.pdf
The asterisk wildcard is versatile and can be used at any position in the pattern. For example, *1* would match any file that has "1" anywhere in its name.
Using Wildcards with the Question Mark (?)
The question mark ? wildcard represents exactly one character. It's useful when you want to match files with similar names but differ by a single character at a specific position.
Let's explore how the question mark wildcard works:
Make sure you're still in the
wildcard_practicedirectory:cd ~/project/wildcard_practiceLet's create some additional files to better demonstrate the question mark wildcard:
touch file-a.txt file-b.txt file-c.txt test1.log test2.log test3.logTo list all the "file-?.txt" files, where ? is a single character:
ls file-?.txtThis command should display:
file-a.txt file-b.txt file-c.txtSimilarly, to list all the "test?.log" files:
ls test?.logThe output should be:
test1.log test2.log test3.logYou can combine multiple question marks to match multiple single characters. For example, to match all text files with names of the form "file?":
ls file?.txtThis should show:
file1.txt file2.txt file3.txtThe question mark wildcard is especially useful when you need to match a specific pattern with variations in exact positions. For instance, if you want files that end with a single-digit number followed by .log:
ls test?.log
The question mark wildcard is more specific than the asterisk since it matches exactly one character, while the asterisk can match any number of characters.
Using Square Brackets [] for Character Classes
The square brackets [] wildcard allows you to specify a set or range of characters to match at a specific position in a file name. This provides more precision than the asterisk or question mark wildcards.
Let's practice using the square brackets wildcard:
Ensure you're still in the
wildcard_practicedirectory:cd ~/project/wildcard_practiceLet's create some additional files to work with:
touch data-1.csv data-2.csv data-3.csv data-a.csv data-b.csvTo list files that have a digit in a specific position, you can use the bracket notation with a range:
ls data-[1-3].csvThis should display:
data-1.csv data-2.csv data-3.csvSimilarly, to list files that have a letter in that position:
ls data-[a-z].csvThis should show:
data-a.csv data-b.csvYou can also list specific characters within the brackets. For example, to list files with either '1' or 'a' after "data-":
ls data-[1a].csvThis should display:
data-1.csv data-a.csvThe caret
^symbol within square brackets negates the match. To list all "data-" files except those ending with "1" or "2":ls data-[^12].csvThis command should show:
data-3.csv data-a.csv data-b.csv
Square brackets offer precise control when you need to match files that differ by specific characters at specific positions.
Practical File Operations with Wildcards
Now that we understand different types of wildcards, let's use them to perform common file operations such as copying, moving, and removing files.
First, make sure you're in the
wildcard_practicedirectory:cd ~/project/wildcard_practiceLet's create a backup directory to store copies of our files:
mkdir backupCopy all text files to the backup directory using the asterisk wildcard:
cp *.txt backup/Let's verify that the files were copied correctly:
ls backup/You should see all the text files in the backup directory:
file-a.txt file-b.txt file-c.txt file1.txt file2.txt file3.txtNow, let's create another directory for organizing different file types:
mkdir csv_files pdf_files log_filesMove all CSV files to the csv_files directory:
mv *.csv csv_files/Move all PDF files to the pdf_files directory:
mv *.pdf pdf_files/Move all log files to the log_files directory:
mv *.log log_files/Let's check that each directory contains the correct files:
echo "CSV Files:" ls csv_files/ echo "PDF Files:" ls pdf_files/ echo "Log Files:" ls log_files/The output should show the appropriate files in each directory.
Finally, let's clean up by removing all backup text files that have a number in their name:
rm backup/file[0-9].txtVerify that only the letter-named text files remain in the backup directory:
ls backup/The output should show:
file-a.txt file-b.txt file-c.txt
These examples demonstrate how wildcards can greatly simplify file management tasks that would otherwise require multiple commands or manual handling of individual files.
Summary
In this lab, you have learned how to use Linux wildcards to efficiently manage and manipulate files. The key concepts covered include:
- Using the asterisk (*) wildcard to match any number of characters
- Using the question mark (?) wildcard to match exactly one character
- Using square brackets [] to match specific characters or ranges
- Applying wildcards to common file operations such as listing, copying, and removing files
These wildcard techniques are essential tools for any Linux user, saving time and reducing errors when working with multiple files. They allow you to perform complex file operations with simple, concise commands rather than having to handle each file individually.
As you continue to work with Linux systems, you will find numerous opportunities to apply these wildcard patterns to streamline your workflow and increase your productivity.



