Bash Wildcard Tutorial

29/12/2020
When we need to search for anything using shell commands then we need to define a pattern for searching. Wildcard characters are used to define the pattern for searching or matching text on string data in the bash shell. Another common use of wildcard characters is to create regular expressions. How you can use different types of wildcard characters for searching files is shown in this tutorial.

Wildcard characters:

The three main wildcard characters are,

  • Star or Asterisk (*)
  • Question mark (?)
  • Square brackets ([])

Asterisk (*) is used to search for particular character(s) for zero or more times. Question mark (?) is used to search for a fixed number of characters where each question mark (?) indicates each character. Square brackets are used to match with the characters of a defined range or a group of characters. The uses of these characters are shown in the next part of this tutorial.

Use of asterisk (*)

Asterisk (*) can be used in various ways with shell commands for searching files. Different use of asterisk (*) are shown in the following examples.

Example – 1: Searching specific file with filename and ‘*’

‘ls’ command is used to find out the list of files and folders of the current directory. ‘ls s*’ command will search all files whose name start with ‘s’

$ ls
$ ls s*

Example – 2: Searching file with particular extension and ‘*’

You can search any file by using asterisk (*) and the file extension. If you want to search all files with ‘.sh’ extension from the current directory then run the following command from the terminal. Here, the filename can be any character(s) and any number of characters.

$ ls *.sh

You can also search files of different extensions by using aterisk (*). The following command will search any files with extension ‘.bash’ or ‘.PNG’

$ ls *.bash *.PNG

Example – 3: Removing file by partial match and ‘*’

You can use asterisk (*) for matching any filename partially. The following command will remove the file which contains ‘img’ word in any part of the filename.

$ ls
$ rm *img*
$ ls

Use of question mark (?)

When you know the exact numbers of characters that you want to search then question mark (?) wildcard can be used.  The following examples show the different use of question mark (?) wildcard.

Example – 1: Searching file with filename and ‘?’

Suppose, the file extension, the total number of characters of a file and some characters of the file are known, then you can use this wildcard to search the file. The command will search the file which has the extension ‘.PNG’, the word ‘pic’ is at the end of the filename and filename is six character long.

$ ls
$ ls ???pic.PNG

Example -2: Searching file with extension and ‘?’

Suppose, you know the filename and the total number of characters of the file extension then you can use question mark (?) wildcard to search the file. The following command will search the file with filename ‘mypic’ and the extension is three characters long.

$ ls
$ ls mypic.???

Use of square brackets ([])

Different range of characters or group of characters can be used within square brackets ([]) for searching files based on the range.

Example -1: Search files of any extension with two ranges values

The following command will search any file whose name contains any character within ‘p-z’ and any digit within ‘0-9’ and the file extension can be any characters.

$ ls
$ ls [p-z0-9]*.*

The basic use of wildcard characters is shown in this tutorial. You can also use wildcard characters for creating any regular expression pattern for doing validation work.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Ansible Archive and Unarchive

Ansible is a great tool to automate your configuration management. The benefit of Ansible is that you don’t need to set...
29/12/2020

How to compare strings in Bash

For different programming purposes, we need to compare the value of two strings. Built-in functions are used in many programming...
28/12/2020

How to Search for your Files on the Linux Command Line

For a Linux desktop, a user can easily install an app to search their files and folders in the file system, but another...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Hướng dẫn fake ip bằng phần mềm SStap
10/06/2025

VPS treo game là gì? Thuê VPS treo game giá rẻ, không lo giật lag
02/06/2025

 BitBrowser – Best Anti-Detect Browser!
26/05/2025

Dịch Vụ Xây Dựng Hệ Thống Peering Với Internet Exchange (IXP)
04/04/2025

Dịch Vụ Triển Khai VPN Site-to-Site & Remote Access
04/04/2025