• Contact
  • Contact Us
  • Disclamer
  • Home 1
  • Home 2
  • Home 3
  • Privacy Policy
Friday, May 23, 2025
How VPS - How to use/setup VPS
  • Login
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon
No Result
View All Result
How VPS - How to use/setup VPS
No Result
View All Result
Home Operating System Linux

10 Practical Examples Using Wildcards to Match Filenames in Linux

How VPS by How VPS
November 2, 2018
in Linux, Operating System
0
0
SHARES
9
VIEWS
Share on FacebookShare on Twitter

Contents

  1. How to Match Filenames Using Wildcards in Linux
  2. How to Combine Wildcards to Match Filenames in Linux
  3. How to Match Characters Set in Linux
  4. How to Negate a Set of Characters in Linux

Wildcards (also referred to as meta characters) are symbols or special characters that represent other characters. You can use them with any command such as ls command or rm command to list or remove files matching a given criteria, receptively.

Read Also: 10 Useful Practical Examples on Chaining Operators in Linux

These wildcards are interpreted by the shell and the results are returned to the command you run. There are three main wildcards in Linux:

  • An asterisk (*) – matches one or more occurrences of any character, including no character.
  • Question mark (?) – represents or matches a single occurrence of any character.
  • Bracketed characters ([ ]) – matches any occurrence of character enclosed in the square brackets. It is possible to use different types of characters (alphanumeric characters): numbers, letters, other special characters etc.

You need to carefully choose which wildcard to use to match correct filenames: it is also possible to combine all of them in one operation as explained in the examples below.

How to Match Filenames Using Wildcards in Linux

For the purpose of this article, we will use following files to demonstrate each example.

createbackup.sh  list.sh  lspace.sh        speaker.sh
listopen.sh      lost.sh  rename-files.sh  topprocs.sh

1. This command matches all files with names starting with l (which is the prefix) and ending with one or more occurrences of any character.

$ ls -l l*	
10 Practical Examples Using Wildcards to Match Filenames in Linux

List Files with Character

2. This example shows another use of * to copy all filenames prefixed with users-0 and ending with one or more occurrences of any character.

$ mkdir -p users-info
$ ls users-0*
$ mv -v users-0* users-info/	# Option -v flag enables verbose output
10 Practical Examples Using Wildcards to Match Filenames in Linux

List and Copy All Files

3. The following command matches all files with names beginning with l followed by any single character and ending with st.sh (which is the suffix).

$ ls l?st.sh	
10 Practical Examples Using Wildcards to Match Filenames in Linux

Match File with Character Name

4. The command below matches all files with names starting with l followed by any of the characters in the square bracket but ending with st.sh.

$ ls l[abdcio]st.sh 
10 Practical Examples Using Wildcards to Match Filenames in Linux

Matching Files with Names

How to Combine Wildcards to Match Filenames in Linux

You can combine wildcards to build a complex filename matching criteria as described in the following examples.

5. This command will match all filenames prefixed with any two characters followed by st but ending with one or more occurrence of any character.

$ ls
$ ls ??st*
10 Practical Examples Using Wildcards to Match Filenames in Linux

Match File Names with Prefix

6. This example matches filenames starting with any of these characters [clst] and ending with one or more occurrence of any character.

$ ls
$ ls [clst]*
10 Practical Examples Using Wildcards to Match Filenames in Linux

Match Files with Characters

7. In this examples, only filenames starting with any of these characters [clst] followed by one of these [io] and then any single character, followed by a t and lastly, one or more occurrence of any character will be listed.

$ ls
$ ls [clst][io]?t*
10 Practical Examples Using Wildcards to Match Filenames in Linux

List Files with Multiple Characters

8. Here, filenames prefixed with one or more occurrence of any character, followed by the letters tar and ending with one or more occurrence of any character will be removed.

$ ls
$ rm *tar*
$ ls
10 Practical Examples Using Wildcards to Match Filenames in Linux

Remove Files with Character Letters

How to Match Characters Set in Linux

9. Now lets look at how to specify a set of characters. Consider the filenames below containing system users information.

$ ls

users-111.list  users-1AA.list  users-22A.list  users-2aB.txt   users-2ba.txt
users-111.txt   users-1AA.txt   users-22A.txt   users-2AB.txt   users-2bA.txt
users-11A.txt   users-1AB.list  users-2aA.txt   users-2ba.list
users-12A.txt   users-1AB.txt   users-2AB.list  users-2bA.list

This command will match all files whose name starts with users-i, followed by a number, a lower case letter or number, then a number and ends with one or more occurrences of any character.

$ ls users-[0-9][a-z0-9][0-9]*

The next command matches filenames beginning with users-i, followed by a number, a lower or upper case letter or number, then a number and ends with one or more occurrences of any character.

$ ls users-[0-9][a-zA-Z0-9][0-9]*

This command that follows will match all filenames beginning with users-i, followed by a number, a lower or upper case letter or number, then a lower or upper case letter and ends with one or more occurrences of any character.

$ ls users-[0-9][a-zA-Z0-9][a-zA-Z]*
10 Practical Examples Using Wildcards to Match Filenames in Linux

Match Characters in Filenames

How to Negate a Set of Characters in Linux

10. You can as well negate a set of characters using the ! symbol. The following command lists all filenames starting with users-i, followed by a number, any valid file naming character apart from a number, then a lower or upper case letter and ends with one or more occurrences of any character.

$ ls users-[0-9][!0-9][a-zA-Z]*

That’s all for now! If you have tried out the above examples, you should now have a good understanding of how wildcards work to match filenames in Linux.

You might also like to read these following articles that shows examples of using wildcards in Linux:

  1. How to Extract Tar Files to Specific or Different Directory in Linux
  2. 3 Ways to Delete All Files in a Directory Except One or Few Files with Extensions
  3. 10 Useful Tips for Writing Effective Bash Scripts in Linux
  4. How to Use Awk and Regular Expressions to Filter Text or String in Files

If you have any thing to share or a question(s) to ask, use the comment form below.

Source: tecmint.com

Tags: Linux Commandslinux guidelinux vps setup guide
Previous Post

A Complete Guide to Usage of ‘usermod’ command – 15 Practical Examples with Screenshots

Next Post

35 Practical Examples of Linux Find Command

Next Post

35 Practical Examples of Linux Find Command

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

No Result
View All Result

Recent Post

Install Imagemagick on CentOS
CentOS

Install Imagemagick on CentOS

by How VPS
June 28, 2023
0

This is how I installed Imagemagick on a vanilla CentOS server Start off by installing the prerequisites yum install php-pear...

Read more
how to Check phpinfo

How to Check phpinfo of Hosting or VPS?

June 28, 2023
Failed to download metadata for repo 'appstream' on Centos 8

How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

February 25, 2022
How to Fix MySQL Error "Plugin 'InnoDB' registration as a STORAGE ENGINE failed"?

How to Fix MySQL Error “Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed”?

November 17, 2020
How to optimize Mysql or MariaDB

How to optimize Mysql or MariaDB

November 3, 2020

Recent News

  • Install Imagemagick on CentOS
  • How to Check phpinfo of Hosting or VPS?
  • How to fix error: Failed to download metadata for repo ‘appstream’ on Centos 8

Category

  • Arch
  • Authentication
  • Backups
  • BSD
  • Centmin Mod
  • CentOS
  • Control Panels
  • CoreOS
  • CWP
  • Debian
  • Directadmin
  • Encryption
  • Fedora
  • Firewalls
  • Hocvps Script
  • Hosting providers
  • Kloxo-MR
  • Linux
  • Mitigations
  • Operating System
  • Plesk
  • Reviews
  • Securing VPS/Servers
  • Security Patches
  • SSL Certificates
  • Uncategorized
  • Upgrading
  • VPS/Servers management guides
  • Vulnerability Detection
  • Web servers software
  • Webhosting Control Panel
  • About
  • Advertise
  • Careers
  • Contact

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

No Result
View All Result
  • Home
  • Management guides
    • Web servers software
      • Directadmin
      • Hocvps Script
      • Centmin Mod
      • CWP
      • Kloxo-MR
      • Plesk
    • Control Panels
    • Securing VPS/Servers
      • SSL Certificates
      • Upgrading
      • Authentication
  • Operating System
    • CentOS
    • Fedora
    • Debian
    • Linux
    • Arch
    • BSD
    • CoreOS
  • Reviews
  • Coupon
    • Domain Coupon
    • Hosting Coupon

© 2025 JNews - Premium WordPress news & magazine theme by Jegtheme.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
Thabet