How to use wildcards to match file names in Linux

Searching for files in Linux can 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 having to type each name individually. This article will explain how to use wildcards in Linux to efficiently match file names.

  • File Management in Unix/Linux

1. Asterisk (*)

The asterisk (*) is a Linux wildcard that matches zero or more characters in a file or directory name. It helps to search for, list, or manipulate multiple files at once. It is often used with commands like cp, mv, and rm to perform batch operations.

Match files by extension

We can execute the command ls *.txt to match all file names with the .txt extension:

Match files by prefix

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

 

Match files by suffix

To list or modify files with a certain extension like '_1', use the ls *_ command :

Match files containing a specific word

We can match file names containing a specific substring by using the asterisk wildcard. For example, the ls *ample* command lists all file names containing the substring 'ample':

Match hidden files

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

2. Question mark (?)

The question mark (?) wildcard is used to match a single character in a file name. It helps to find files whose names follow a specific pattern but differ by one character. It is often used to find or manage files with similar names but differ by a single character. For example, file?.txt matches "file1.txt", "fileA.txt", "fileB.txt", etc.

Matches files that have any single character at a specific position

We can use the question mark (?) wildcard to match file names where the specified position can be any single character. For example, the command ls file?.txt matches any file name that begins with file, followed by any single character, and ends with the .txt extension:

 

Match files with a fixed number of characters

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

Combine the wildcard character ? with *

We can combine the ? wildcard with the * wildcard to do some advanced pattern matching. For example, the pattern ?ile* matches a filename whose first character can be any character, followed by "ile", then any number of characters:

3. Square brackets ([ ])

Square brackets ([ ]) match any character enclosed within the brackets. You can include different types of characters, such as letters, numbers, or special symbols, to specify a specific set of matches. For example, the command ls [1ab]file.txt lists all files that begin with 1, a, or b, followed by "file.txt":

4. Exclamation mark (!)

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

5. Curly brackets ({ })

Curly braces ({ }), also known as range expansions, allow us to specify multiple patterns separated by commas. They expand to specific file names instead of acting as wildcards. For example, the command ls file{1,2,3}.txt is equivalent to ls file1.txt file2.txt file3.txt . This command lists all of these specific files if they exist:

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 selection based on patterns. For example, we use the command find Documents -name "*.txt" to locate all .txt files in the Documents directory :

 

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

7. Use wildcards with case-sensitive file names

Wildcards in Linux are case sensitive, meaning file names with different letters are treated as separate. To match both uppercase and lowercase variants, we can use character classes or the case-insensitive option in the command.

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

Now you know how to use wildcards to manage files in Linux faster and easier. Whether you're searching for files, organizing folders, or automating tasks, these wildcard techniques will save you time and effort.

We recommend starting with * and ? as these are the most commonly used. Then experiment with square brackets and curly braces to refine your search. Once you're comfortable, explore regular expressions for more advanced pattern matching.

You've just finished reading the article "How to use wildcards to match file names in Linux" edited by the TipsMake team. We hope this article has provided you with many useful tech tips and tricks. You can search for similar articles on tips and guides. Thank you for reading and for following us regularly.

Close
Category

System

Windows XP

Windows Server 2012

Windows 8

Windows 7

Windows 10

Wifi tips

Virus Removal - Spyware

Speed ​​up the computer

Server

Security solution

Mail Server

LAN - WAN

Ghost - Install Win

Fix computer error

Configure Router Switch

Computer wallpaper

Computer security

Mac OS X

Mac OS System software

Mac OS Security

Mac OS Office application

Mac OS Email Management

Mac OS Data - File

Mac hardware

Hardware

USB - Flash Drive

Speaker headset

Printer

PC hardware

Network equipment

Laptop hardware

Computer components

Advice Computer

Game

PC game

Online game

Mobile Game

Pokemon GO

information

Technology story

Technology comments

Quiz technology

New technology

British talent technology

Attack the network

Artificial intelligence

Technology

Smart watches

Raspberry Pi

Linux

Camera

Basic knowledge

Banking services

SEO tips

Science

Strange story

Space Science

Scientific invention

Science Story

Science photo

Science and technology

Medicine

Health Care

Fun science

Environment

Discover science

Discover nature

Archeology

Life

Travel Experience

Tips

Raise up child

Make up

Life skills

Home Care

Entertainment

DIY Handmade

Cuisine

Christmas

Application

Web Email

Website - Blog

Web browser

Support Download - Upload

Software conversion

Social Network

Simulator software

Online payment

Office information

Music Software

Map and Positioning

Installation - Uninstall

Graphic design

Free - Discount

Email reader

Edit video

Edit photo

Compress and Decompress

Chat, Text, Call

Archive - Share

Electric

Water heater

Washing machine

Television

Machine tool

Fridge

Fans

Air conditioning

Program

Unix and Linux

SQL Server

SQL

Python

Programming C

PHP

NodeJS

MongoDB

jQuery

JavaScript

HTTP

HTML

Git

Database

Data structure and algorithm

CSS and CSS3

C ++

C #

AngularJS

Mobile

Wallpapers and Ringtones

Tricks application

Take and process photos

Storage - Sync

Security and Virus Removal

Personalized

Online Social Network

Map

Manage and edit Video

Data

Chat - Call - Text

Browser and Add-on

Basic setup