Skip to main content

Command Palette

Search for a command to run...

Tips for Mastering Wildcards in Linux :

Easy Steps to Use Wildcards in Linux :

Updated
โ€ข4 min read
Tips for Mastering Wildcards in Linux :
R

A ๐Ÿš€ Passionate Linux and Cloud Computing Student . ๐ŸŒ Enthusiast in DevOps and System Administration ๐Ÿง‘โ€๐Ÿ’ป.

Linux wildcards are special characters that help you match patterns in file and directory names. They are incredibly powerful and can save you a lot of time when performing operations in the terminal. In this guide, weโ€™ll explore what wildcards are, how they work, and how to use them with various commands.

What Are Linux Wildcards ?

Wildcards are symbols that represent one or more characters in file and directory names. They allow you to specify a set of files or directories without typing each name individually. The most common wildcards in Linux are the asterisk (*), question mark (?), and square brackets ([]).

Key Wildcards and Their Usage :

The Asterisk (*) :

  • What is it ? The asterisk (*) matches zero or more characters in a filename or directory name.

  • How it works : Itโ€™s a catch-all wildcard that can represent any combination of characters, including none.

  • Example :

      bashCopy code# List all files in the directory
      ls *
    
      # List all files ending with .txt
      ls *.txt
    
      # List all files starting with "file"
      ls file*
    

The Question Mark (?) :

  • What is it ? The question mark (?) matches exactly one character in a filename or directory name.

  • How it works : Itโ€™s useful for matching filenames where you know the exact number of characters.

  • Example :

      bashCopy code# List files with exactly one character before .txt
      ls ?.txt
    
      # List files with exactly five characters
      ls ?????
    

Square Brackets ([]) :

  • What is it ? Square brackets ([]) allow you to specify a range or a set of characters to match.

  • How it works : You can match any one character within the brackets.

  • Example :

      bashCopy code# List files starting with a, b, or c
      ls [abc]*
    
      # List files starting with any vowel
      ls [aeiou]*
    
      # List files ending with 1, 2, or 3
      ls *[123]
    

Curly Braces ({}) :

  • What is it ? Curly braces ({}) allow you to specify a comma-separated list of strings to match.

  • How it works : Itโ€™s useful for generating multiple patterns.

  • Example :

      bashCopy code# List files ending with .txt, .doc, or .pdf
      ls *.{txt,doc,pdf}
    
      # Create directories dir1, dir2, and dir3
      mkdir dir{1,2,3}
    

Using Wildcards with Linux Commands :

Wildcards can be used with a variety of Linux commands to perform operations on multiple files or directories at once.

List Files with ls :

bashCopy code# List all files
ls *

# List all files starting with "test"
ls test*

# List all files with a single character name
ls ?

Copy Files with cp :

bashCopy code# Copy all .txt files to the backup directory
cp *.txt backup/

# Copy files starting with "log" to the logs directory
cp log* logs/

Move Files with mv :

bashCopy code# Move all .jpg files to the images directory
mv *.jpg images/

# Move files with exactly five characters in their name
mv ????? destination/

Remove Files with rm :

bashCopy code# Remove all .tmp files
rm *.tmp

# Remove files starting with "temp"
rm temp*

# Remove files ending with 1, 2, or 3
rm *[123]

Practical Examples :

Here are some practical examples of how to use wildcards effectively:

  1. Find and Remove Backup Files :

     bashCopy code# List and then remove all backup files ending with ~
     ls *~
     rm *~
    
  2. Batch Rename Files :

     bashCopy code# Rename all .htm files to .html
     for file in *.htm; do
       mv "$file" "${file%.htm}.html"
     done
    
  3. Copy Specific Files :

     bashCopy code# Copy all text and document files to the docs directory
     cp *.{txt,doc} docs/
    
  4. Search for Files :

     bashCopy code# Find all image files in the current directory
     find . -name "*.jpg"
    

Conclusion :

Linux wildcards are powerful tools that make it easy to work with multiple files and directories. By mastering the asterisk (*), question mark (?), square brackets ([]), and curly braces ({}), you can perform complex file manipulations with simple commands. Whether you're listing, copying, moving, or deleting files, wildcards can save you a lot of time and effort. Happy wild-carding! ๐ŸŒŸ

Got questions or need further clarification? Drop a comment below. Happy redirecting and streamlining your Linux journey! ๐Ÿš€

Thank You ๐Ÿ™โค๏ธ๐Ÿ˜Š.

Linux Basics To Advance

Part 5 of 9

In This Series I Will Upload Linux Blogs From Basic To Advance. Stay Tuned๐Ÿ™โค๏ธ๐Ÿ˜Š .

Up next

How to Master Linux Archives. Tar, Gzip, and Gunzip Explained :

Complete Guide to Understanding Linux Archives