Linux Wildcard Character

LinuxLinuxBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/FileandDirectoryManagementGroup(["File and Directory Management"]) linux/BasicFileOperationsGroup -.-> linux/ls("Content Listing") linux/BasicFileOperationsGroup -.-> linux/cp("File Copying") linux/BasicFileOperationsGroup -.-> linux/mv("File Moving/Renaming") linux/BasicFileOperationsGroup -.-> linux/rm("File Removing") linux/FileandDirectoryManagementGroup -.-> linux/cd("Directory Changing") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("Directory Creating") linux/FileandDirectoryManagementGroup -.-> linux/wildcard("Wildcard Character") subgraph Lab Skills linux/ls -.-> lab-271447{{"Linux Wildcard Character"}} linux/cp -.-> lab-271447{{"Linux Wildcard Character"}} linux/mv -.-> lab-271447{{"Linux Wildcard Character"}} linux/rm -.-> lab-271447{{"Linux Wildcard Character"}} linux/cd -.-> lab-271447{{"Linux Wildcard Character"}} linux/mkdir -.-> lab-271447{{"Linux Wildcard Character"}} linux/wildcard -.-> lab-271447{{"Linux Wildcard Character"}} end

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:

  1. First, open your terminal and navigate to the project directory:

    cd ~/project
  2. Create a practice directory for this lab and navigate into it:

    mkdir -p wildcard_practice
    cd wildcard_practice
  3. Now, let's create some sample files to work with:

    touch file1.txt file2.txt file3.txt document1.pdf document2.pdf image.jpg
  4. Use the ls command to list all files in the directory:

    ls

    You should see all the files you just created:

    document1.pdf  document2.pdf  file1.txt  file2.txt  file3.txt  image.jpg
  5. Now, let's use the asterisk wildcard to list only the text files:

    ls *.txt

    This command should display only the files ending with .txt:

    file1.txt  file2.txt  file3.txt
  6. You 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.txt
  7. To list all PDF documents:

    ls *.pdf

    This 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:

  1. Make sure you're still in the wildcard_practice directory:

    cd ~/project/wildcard_practice
  2. Let'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.log
  3. To list all the "file-?.txt" files, where ? is a single character:

    ls file-?.txt

    This command should display:

    file-a.txt  file-b.txt  file-c.txt
  4. Similarly, to list all the "test?.log" files:

    ls test?.log

    The output should be:

    test1.log  test2.log  test3.log
  5. You 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?.txt

    This should show:

    file1.txt  file2.txt  file3.txt
  6. The 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:

  1. Ensure you're still in the wildcard_practice directory:

    cd ~/project/wildcard_practice
  2. Let's create some additional files to work with:

    touch data-1.csv data-2.csv data-3.csv data-a.csv data-b.csv
  3. To list files that have a digit in a specific position, you can use the bracket notation with a range:

    ls data-[1-3].csv

    This should display:

    data-1.csv  data-2.csv  data-3.csv
  4. Similarly, to list files that have a letter in that position:

    ls data-[a-z].csv

    This should show:

    data-a.csv  data-b.csv
  5. You can also list specific characters within the brackets. For example, to list files with either '1' or 'a' after "data-":

    ls data-[1a].csv

    This should display:

    data-1.csv  data-a.csv
  6. The caret ^ symbol within square brackets negates the match. To list all "data-" files except those ending with "1" or "2":

    ls data-[^12].csv

    This 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.

  1. First, make sure you're in the wildcard_practice directory:

    cd ~/project/wildcard_practice
  2. Let's create a backup directory to store copies of our files:

    mkdir backup
  3. Copy all text files to the backup directory using the asterisk wildcard:

    cp *.txt backup/
  4. 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.txt
  5. Now, let's create another directory for organizing different file types:

    mkdir csv_files pdf_files log_files
  6. Move all CSV files to the csv_files directory:

    mv *.csv csv_files/
  7. Move all PDF files to the pdf_files directory:

    mv *.pdf pdf_files/
  8. Move all log files to the log_files directory:

    mv *.log log_files/
  9. 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.

  10. Finally, let's clean up by removing all backup text files that have a number in their name:

    rm backup/file[0-9].txt
  11. Verify 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:

  1. Using the asterisk (*) wildcard to match any number of characters
  2. Using the question mark (?) wildcard to match exactly one character
  3. Using square brackets [] to match specific characters or ranges
  4. 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.