14 Wildcards Simple (But Important) Things To Remember Linux Scripting

The shells provide a number of 14 wildcards that you can use to abbreviate filenames or refer to groups of files. For example, let’s say you want to delete all filenames ending in .txt in the current directory. You could delete these files one by one, but that would be boring if there were only 10 and very boring and time waste if there were 100. Instead, you can use a 14 wildcards name to say, “I want delete all files whose names end with .txt, regardless of what the first part is” The wildcard is the “regardless” part. Like a wildcard in a filename can have any value.

What is  wildcard characters..?

Wildcards are the characters which replaces single or multiple characters in place of any.

Let’s See 14 Wildcards simple examples

1. Any Single Character

Fetching the data appropriate and exactly required, we are going to see how ? (question mark) replaces single character 14 wildcards

[root@TechTutorials wildcard]# ls -l ??
-rw-r--r--. 1 root root 0 Jul 24 13:27 ak
-rw-r--r--. 1 root root 0 Jul 24 13:27 er
-rw-r--r--. 1 root root 0 Jul 24 13:27 fh
-rw-r--r--. 1 root root 0 Jul 24 13:27 gd
-rw-r--r--. 1 root root 0 Jul 24 13:27 iy
-rw-r--r--. 1 root root 0 Jul 24 13:27 pi
-rw-r--r--. 1 root root 0 Jul 24 13:27 qs
-rw-r--r--. 1 root root 0 Jul 24 13:27 ry
-rw-r--r--. 1 root root 0 Jul 24 13:27 wq
[root@TechTutorials wildcard]# ls -l a?
-rw-r--r--. 1 root root 0 Jul 24 13:27 ak

Above is an command example where there is ? (question mark) which replaces single character

2. Any string of 0 or more characters

*(Star) replaces multiple characters Let’s see an example

[root@TechTutorials wildcard]# ls -l B*
-rw-r--r--. 1 root root 0 Jul 24 13:23 Bat
[root@TechTutorials wildcard]# ls -l 1*
-rw-r--r--. 1 root root 0 Jul 24 13:16 1234
-rw-r--r--. 1 root root 0 Jul 24 13:15 12ABC
-rw-r--r--. 1 root root 0 Jul 24 13:16 12hh23

3. one Character within enclosed class

Here is an example we want to match the multiple characters one by one and fetch related data

[root@TechTutorials wildcard]# ls -l [abc]*
-rw-r--r--. 1 root root 0 Jul 24 13:15 abc
-rw-r--r--. 1 root root 0 Jul 24 13:15 abc12
-rw-r--r--. 1 root root 0 Jul 24 13:27 ak
-rw-r--r--. 1 root root 0 Jul 24 13:23 bAT
-rw-r--r--. 1 root root 0 Jul 24 13:27 cxs
[root@TechTutorials wildcard]# ls -l [123]*
-rw-r--r--. 1 root root 0 Jul 24 13:16 1234
-rw-r--r--. 1 root root 0 Jul 24 13:15 12ABC
-rw-r--r--. 1 root root 0 Jul 24 13:16 12hh23

4. Character not in enclosed class

Now we are going to see characters not in the enclosed class will be displayed. Below example abc is used, in list of files there is no abc file. 14 wildcards

[root@TechTutorials wildcard]# ls -l [!abc]*
-rw-r--r--. 1 root root 0 Jul 24 13:16 1234
-rw-r--r--. 1 root root 0 Jul 24 13:15 12ABC
-rw-r--r--. 1 root root 0 Jul 24 13:16 12hh23
-rw-r--r--. 1 root root 0 Jul 24 13:26 876a.tar
-rw-r--r--. 1 root root 0 Jul 24 13:26 876b.tar
-rw-r--r--. 1 root root 0 Jul 24 13:26 876c.tar
-rw-r--r--. 1 root root 0 Jul 24 13:15 ABC
-rw-r--r--. 1 root root 0 Jul 24 13:24 ABC1.txt
-rw-r--r--. 1 root root 0 Jul 24 13:24 ABC2.txt
-rw-r--r--. 1 root root 0 Jul 24 13:24 ABC3.txt

Note: ! = not equal to

5. List all alpha characters

without using any special character just fetch only alpha characters, use [[:alpha:]]

[root@TechTutorials wildcard]# ls -l [[:alpha:]]*
-rw-r--r--. 1 root root 0 Jul 24 13:15 abc
-rw-r--r--. 1 root root 0 Jul 24 13:15 ABC
-rw-r--r--. 1 root root 0 Jul 24 13:15 abc12
-rw-r--r--. 1 root root 0 Jul 24 13:24 ABC1.txt
-rw-r--r--. 1 root root 0 Jul 24 13:24 ABC2.txt
-rw-r--r--. 1 root root 0 Jul 24 13:24 ABC3.txt
-rw-r--r--. 1 root root 0 Jul 24 13:27 ak

6. List not alpha characters

Using ! (exclamatory) character within enclosed class along with specified method, which will fetch not alpha characters

[root@TechTutorials wildcard]# ls -l [![:alpha:]]*
-rw-r--r--. 1 root root 0 Jul 24 13:16 1234
-rw-r--r--. 1 root root 0 Jul 24 13:15 12ABC
-rw-r--r--. 1 root root 0 Jul 24 13:16 12hh23
-rw-r--r--. 1 root root 0 Jul 24 13:26 876a.tar
-rw-r--r--. 1 root root 0 Jul 24 13:26 876b.tar
-rw-r--r--. 1 root root 0 Jul 24 13:26 876c.tar

7. Listing lower case file name files

Listing all lower character files by using [[:lower:]] wildcard

[root@TechTutorials wildcard]# ls -l [[:lower:]]*
-rw-r--r--. 1 root root 0 Jul 24 13:15 abc
-rw-r--r--. 1 root root 0 Jul 24 13:15 abc12
-rw-r--r--. 1 root root 0 Jul 24 13:27 ak
-rw-r--r--. 1 root root 0 Jul 24 13:23 bAT
-rw-r--r--. 1 root root 0 Jul 24 13:27 cxs
-rw-r--r--. 1 root root 0 Jul 24 13:27 er
-rw-r--r--. 1 root root 0 Jul 24 13:27 fh
-rw-r--r--. 1 root root 0 Jul 24 13:27 gd
-rw-r--r--. 1 root root 0 Jul 24 13:27 i
-rw-r--r--. 1 root root 0 Jul 24 13:27 iy

8. Upper case character file Names 

using [[:upper:]] will fetch only upper case characters

[root@TechTutorials wildcard]# ls -l [[:upper:]]*
-rw-r--r--. 1 root root 0 Jul 24 13:15 ABC
-rw-r--r--. 1 root root 0 Jul 24 13:24 ABC1.txt
-rw-r--r--. 1 root root 0 Jul 24 13:24 ABC2.txt
-rw-r--r--. 1 root root 0 Jul 24 13:24 ABC3.txt
-rw-r--r--. 1 root root 0 Jul 24 13:23 Bat
-rw-r--r--. 1 root root 0 Jul 24 13:23 CAT

9. Fetch only digits (0-9)

There are so many files in the directory but i want to fetch only files which are starting with numbers 0 to 9

[root@TechTutorials wildcard]# ls -l [[:digit:]]*
-rw-r--r--. 1 root root 0 Jul 24 13:16 1234
-rw-r--r--. 1 root root 0 Jul 24 13:15 12ABC
-rw-r--r--. 1 root root 0 Jul 24 13:16 12hh23
-rw-r--r--. 1 root root 0 Jul 24 13:26 876a.tar
-rw-r--r--. 1 root root 0 Jul 24 13:26 876b.tar
-rw-r--r--. 1 root root 0 Jul 24 13:26 876c.tar

10. Fetch printable characters not a space or alphanumeric

Fetch printable characters not a space or alphanumeric 

[root@TechTutorials wildcard]# ls -l [[:punct:]]*
-rw-r--r--. 1 root root 0 Jul 24 15:09 @@

11. Alpha numeric character 

Example we have below files in current directory

[root@TechTutorials wildcard]# ls -l
total 0
-rw-r--r--. 1 root root 0 Jul 24 15:15 @!
-rw-r--r--. 1 root root 0 Jul 24 15:09 @@
-rw-r--r--. 1 root root 0 Jul 24 13:16 1234
-rw-r--r--. 1 root root 0 Jul 24 13:15 12ABC
-rw-r--r--. 1 root root 0 Jul 24 13:16 12hh23

to fetch only alpha numeric character files just use below method to fetch

[root@TechTutorials wildcard]# ls -l [[:alnum:]]*
-rw-r--r--. 1 root root 0 Jul 24 13:16 1234
-rw-r--r--. 1 root root 0 Jul 24 13:15 12ABC
-rw-r--r--. 1 root root 0 Jul 24 13:16 12hh23

12. Whitespace characters

To fetch the files / file names which has whitespace before the file name will be listed 

[root@TechTutorials wildcard]# ls -l [[:space:]]*
ls: cannot access [[:space:]]*: No such file or directory

There is no whitespace file name exists 

13. AND operator

When executing two commands which are dependant each other we have to use && operator

[root@TechTutorials wildcard]# ls && date
@! 1234 12hh23 876b.tar a ABC ABC1.txt ABC3.txt arkit tech Bat cxs fh i jkl k2.doc klm opy qs ry uiy y
@@ 12ABC 876a.tar 876c.tar abc abc12 ABC2.txt ak bAT CAT er gd iy k1.doc k3.doc mno pi ravi kumar sdu wq
Sun Jul 24 15:25:40 IST 2016
[root@TechTutorials wildcard]# sl && date
bash: sl: command not found...
Similar command is: 'ls'

as per above example if first command is executed successfully then second command will also executes. If first command fails then next command will not execute.

14. OR Operator

OR operator will work in different than AND operator. OR operator will execute any one of the command yet a time, which means if first command fails then it will execute second command. If first command executed successfully then second command will not execute

[root@TechTutorials wildcard]# ls || date
@! 1234 12hh23 876b.tar a ABC ABC1.txt ABC3.txt arkit tech Bat cxs fh i jkl k2.doc klm opy qs ry uiy y
@@ 12ABC 876a.tar 876c.tar abc abc12 ABC2.txt ak bAT CAT er gd iy k1.doc k3.doc mno pi ravi kumar sdu wq
[root@TechTutorials wildcard]# sl || date
bash: sl: command not found...
Similar command is: 'ls'
Sun Jul 24 15:30:16 IST 2016

Conclusion – 14 wildcards

14 wildcards are used to fetch the data as required in situation. 

That’s it about this article

Related Articles

Create multiple user with single command

File System Usage Monitoring Script

Monitoring CPU Utilisation using shell script

Disk Space Monitoring shell Script

8 cut command practical examples explained

Shell Script Class 2 Variables

Thanks for your wonderful Support and Encouragement

Ravi Kumar Ankam

My Name is ARK. Expert in grasping any new technology, Interested in Sharing the knowledge. Learn more & Earn More

1 Response

  1. ozi says:

    Thank you for the article!

Leave a Reply

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