Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

3.3 Wildcards on the command line


May 23, 2021 That's what Linux should learn



Everyone may have encountered the embarrassment of writing a forgetful word, as Linux operations personnel, we sometimes encounter a clear name of a file is in the mouth but just can not remember the situation. I f you remember the first few letters of a file and want to traverse to find all the files that start with this keyword, how do you do that? For example, suppose you want to bulk view the permission properties of all hard disk files, one way is this:

[root@linuxprobe ~]# ls -l /dev/sda brw-rw----. 1 root disk 8, 0 May 4 15:55 /dev/sda [root@linuxprobe ~]# ls -l /dev/sda1 brw-rw----. 1 root disk 8, 1 May 4 15:55 /dev/sda1 [root@linuxprobe ~]# ls -l /dev/sda2 brw-rw----. 1 root disk 8, 2 May 4 15:55 /dev/sda2 [root@linuxprobe ~]# ls -l /dev/sda3 ls: cannot access /dev/sda3: No such file or directory

Fortunately, I only have three hard drive files and partitions, and if there are hundreds, it's estimated that it will take a day to get busy with this thing. T his shows that this approach is indeed inefficient. A lthough we'll cover the storage structure and FHS of Linux systems in Chapter 6, we should now be able to see some simple rules. F or example, these hard disk device files all start with sda and are stored in the /dev directory, so that even if we don't know the partition number of the hard drive and the number of specific partitions, we can use wildcards to handle them. A s the name implies, a wildcard is a common symbol of matching information, such as an asterisk (*) for matching zero or more characters, a question mark (?) for matching a single character, a number in parentheses with a number of 0-9 for a single number between 0 and 9, and a letter in parentheses with a letter of s abc for matching a, b, and c characters. As the saying goes, "A hundred smells is better than a sight, a book is not as good as an experiment", so let's match all the files in the /dev directory that begin with sda:

[root@linuxprobe ~]# ls -l /dev/sda* brw-rw----. 1 root disk 8, 0 May 4 15:55 /dev/sda brw-rw----. 1 root disk 8, 1 May 4 15:55 /dev/sda1 brw-rw----. 1 root disk 8, 2 May 4 15:55 /dev/sda2

What if you only want to see information about a file with a file name sda that starts, but is followed by a file with one other character? At this point, you need to use a question mark to match.

[root@linuxprobe ~]# ls -l /dev/sda? b rw-rw----. 1 root disk 8, 1 May 4 15:55 /dev/sda1 brw-rw----. 1 root disk 8, 2 May 4 15:55 /dev/sda2

In addition to matching a single number between 0 and 9 with a number of 0-9, you can also match only one of the three specified numbers in this way, and if it does not, it will not be displayed:

[root@linuxprobe ~]# ls -l /dev/sda[0-9] brw-rw----. 1 root disk 8, 1 May 4 15:55 /dev/sda1 brw-rw----. 1 root disk 8, 2 May 4 15:55 /dev/sda2 [root@linuxprobe ~]# ls -l /dev/sda[135] brw-rw----. 1 root disk 8, 1 May 4 15:55 /dev/sda1