Sunday, March 9, 2025

How to Use Wildcards to Match Filenames in Linux

https://www.maketecheasier.com/use-wildcards-match-filenames-in-linux

How to Use Wildcards to Match Filenames in Linux

Linux Wildcard

Finding files in Linux might seem confusing at first, but don’t worry, it gets easier once you understand wildcards. Wildcards are special symbols that help you select multiple files or folders without typing each name separately. In this article, we will explain how to use wildcards in Linux to match filenames effectively.

1. Asterisk (*)

The asterisk (*) is a Linux wildcard that matches zero or more characters in filenames or directory names. It helps in searching, listing, or manipulating multiple files at once. It is commonly used with commands like cp, mv, and rm to perform bulk operations.

Matching files by extension

We can execute the ls *.txt command to match all those filenames that end with .txt:

linux command wildcards match files by extension

Matching files by prefix

If you need to list files that start with a word example, you can use the ls example* command:

linux command wildcards Matching files by prefix

Matching files by suffix

To list or modify files that end with a certain pattern like “_1”, use ls *_ command:

linux command wildcards Matching files by suffix

Matching files containing a specific word

We can match filenames containing a specific substring using the asterisk wildcard. For example, the ls *ample* command lists all those filenames that contain a substring “ample”:

linux command wildcards matching using Substring

Matching hidden files

In Linux, hidden files start with a dot. We can use the ls .* command to list hidden files:

linux command wildcards Match Hidden Files

2. Question Mark (?)

The question mark (?) wildcard is used to match a single character in a filename. It helps find files with names that follow a specific pattern but differ by one character. It is commonly used for finding or managing files with similar names but differing by a single character. For example, file?.txt matches “file1.txt,” “fileA.txt,” “fileB.txt,” etc.

Matching files with any single character at a specific position

We can use the question mark (?) wildcard to match filenames where a specific position can be any single character. For example, the ls file?.txt command matches any filename starting with file, followed by any single character, and ending with the .txt extension:

linux command wildcards Match File Specific Character

Matching files with a fixed number of characters

We can use the ? wildcard multiple times to match a fixed number of characters in a file name. For example, the command ls example??.txt matches any file starting with a word example, followed by any two characters, and ending with the .txt extension:

Match Fixed Characters

Combining ? with * wildcard

We can combine ? wildcard with * wildcard to perform some advanced pattern matching. For example, the pattern ?ile* matches filenames where the first character can be anything, followed by “ile”, and then any number of characters:

linux command wildcards Combining multiple wildcards

3. Bracket Expressions ([ ])

Bracketed characters ([ ]) match any character enclosed within the square brackets. You can include various character types, such as letters, numbers, or special symbols, to define a specific matching set. For example, the ls [1ab]file.txt command lists all those files that start with 1, a, or b, followed by “file.txt”:

Bracket Expansion

4. Negation (!)

We can also negate a set of characters using the ! symbol. For example, the ls file[!a-zA-Z] command lists all filenames that start with file, followed by any character except a letter (a-z or A-Z). It matches “file1,” “file_,” or “file@” but not “fileA” or “filez”:

linux command wildcards Negating a set of characters

5. Braces ({ })

Braces ({ }), also known as range expansion, allow us to specify multiple comma-separated patterns. They expand into specific filenames instead of acting as a wildcard. For example, the command ls file{1,2,3}.txt is equivalent to ls file1.txt file2.txt file3.txt. It lists all these specific files if they exist:

linux command wildcards Braces to specify multiple patterns

6. Using Wildcards with Linux Commands

We can use wildcards with various Linux commands like find, ls, cp, and rm to make file management easier by allowing pattern-based selection. For example, we use the find Documents -name "*.txt" command to locate all .txt files in the Documents directory:

Wildcards With Linux Commands

Similarly, we can use wildcards with any other Linux command to achieve a specific purpose.

7. Using Wildcards with Case-Sensitive File Names

Wildcards in Linux are case-sensitive, which means filenames with different letter cases are treated as distinct. To match both uppercase and lowercase variations, we can use character classes or case-insensitive options in commands.

For example, we can use the ls [fF]ile.txt command to match both file.txt and File.txt:

Case sensitive filenames

So there you have it! Now you know how to use wildcards to make file management in Linux faster and easier. Whether you’re searching for files, organizing directories, or automating tasks, these wildcard techniques will save you time and effort.

I recommend starting with * and ? since they’re the most commonly used. Then, experiment with bracket expressions and braces to refine your searches. Once comfortable, explore regular expressions for even more advanced pattern matching.


  • No comments:

    Post a Comment