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

How to Handle Command Line Arguments in a Bash Script

In many cases, bash scripts require argument values to provide input options to the script. You can handle command line...
29/12/2020

Bash for loop examples

Loops are a very essential part of any type of programming or scripting languages. Like any other standard programming,...
29/12/2020

Types of Shells in Linux

Overview In this lesson, we will study the types of shells available in Linux and what advantages one shell offers over...
28/12/2020
Bài Viết

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

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024

Dịch vụ thuê mua proxy giá rẻ an toàn, tốc độ cao
13/05/2024

Thuê mua proxy V6 uy tín, chất lượng tại đâu?
11/05/2024

Thuê mua proxy Tiktok tăng doanh thu, hiệu quả cao
11/05/2024