When we use the command ls to view a directory or file, sometimes we just need to view a specific directory or file, this time we need to use the file name wildcard, also known as the character wildcard.
Special note here, after cd/etc/, go to the ETC directory, when using LS, you need to add the option –d so that the result displays the directory name or file name, does not expand the directory display. Can not be added.
As for the use of the LS command, you can find it yourself.
Next, I'll explain the wildcard characters by example.
1.) *: Used to match 0 or more arbitrary characters
For example, LS * finds files or directories of any character under/etc
[[email protected] etc] # ls-d * 3dkdj maven3fgfj mime.types3fgfj[] mke2fs.conf3jsjdl[] modprobe.dabrt motdacpi mTAB
2.)? : matches any 1 characters, that is, one character matches one character
For example: LS??? Find files or directories with any length of three characters under/etc
[[email protected] etc] # ls-d??? 253 786 Hal JVM LVM opt PPP rpm ssh X11 xml333 GDM IPA KDE NTP PKI RPC SCL SSL xdg Yum
3.) [0-9]: A [0-9] represents only a number between 0-9
For example: LS [0-9] [1-2][0-9] 3[0-9] Use touch {1..100} to create 1 to 100 of these 100 files, showing 1-35
[[email protected] etc] # ls [0-9] [1-2][0-9] [3][0-5]1 each 28 3 to 5 7 910 2 23 4 6 8
4.) [A-Z]: Indicates aabbcc......z, unable to display z [A-z]: indicates ABBCC ... Z, unable to display z [A-z]: denotes all uppercase and lowercase letters
Eg: find a file or directory with a length of three that starts with any letter in the/etc directory
[[email protected] etc] # ls-d [A-z]?? GDM IPA KDE NTP PKI RPC SCL SSL xdg yumhal JVM LVM Opt PPP rpm ssh X11 xml
5.) [AE]: Represents a single character of A or E [^ae]: Indicates that neither a nor E is any other single character
Eg: Show only non-hidden directories in/etc
[[email protected] etc] # ls -D/[^.] */app /lib64 /proc /tmp/bin /lost+found /root /usr/boot /media /sbin /var/dev /misc /selinux /vmwaretools-10.1.6-5214329. tar.gz /etc/mnt /srv /vmware-tools-distrib /home/net /sys /lib /opt /testdir
Eg: Show only non-hidden directories in the user's home directory
1[[Email protected] ~]#ls-1/home/[^.] *2/home/CentOS:3 Desktop4 Documents5 Downloads6 Music7 Pictures8 Public9 TemplatesTen Videos One A/home/www:
6.) [: Lower:]: Indicates lowercase letters
[: Upper:]: Uppercase
[: Alpha:]: Indicates uppercase and lowercase letters
Eg: Displays files or directories with a file name that contains at least one lowercase letter and a number and ends with. conf in the/etc directory
(PS: Option-1 to list the result items for LS lookup)
1 [[email protected] ~]# ls-d1/etc/*[[:lower:]]*[0-9]*.conf2 /etc/ krb5.conf3 /etc/mke2fs.conf4 /etc/pbm2ppa.conf5 /etc/ Pnm2ppa.conf
7.) [:d igit:]: Indicates that any number equals 0-9
[: Alnum:]: Denotes any letter and number equal to a-z0-9 or with [[:d Igit:][:alpha:]] to indicate
Eg: Displays all files or directories that start with an uppercase letter and end with two digits in the/etc directory
1 [[email protected] ~]# ls-d1/etc/[[:upper:]]*[[:d igit:]][[:d igit:]]2 /etc/ Kdhu67 3 /etc/Ldf884 /etc/S00995 /etc/Sdjfdjg9886 /etc /Sdjfdjg98977 /etc/x11
Eg: Displays a file or directory containing symbols and numbers for the file name in the/var/log directory
1 [[email protected] ~]# ls-d1 /var/log/*[[:p unct:]]*[[:d igit:]]* 2 /var/log /btmp-20191120 3 /var/log/cron-20191120 4 /var/log/maillog-20191120 5 /var/log/ messages-20191120 6 /var/log/secure-20191120 7 /var/log/spooler-20191120 8 /var/log/ vmware-vgauthsvc.log.0 9 /var/log/xorg.0. Log /var/log/xorg.0. Log.old /var/log/xorg.9.log
8.) [: blank:]: horizontal white space character
[: Space:]: Horizontal or vertical whitespace character filenames cannot contain carriage returns, so use with [: blank:]: Same effect
9.) [:p UNCT:]: denotes symbol
See examples in usage 7 for instructions on how to use them.
linux--character wildcard characters