Globbing pattern for your shell commands

Nov 9, 2014 4:59 PM

             __    __                      
___________ _/  |__/  |_  ___________  ____ 
\____ \__  \\   __\   __\/ __ \_  __ \/    \
|  |_> > __ \|  |  |  | \  ___/|  | \/   |  \
|   __(____  /__|  |__|  \___  >__|  |___|  /
|__|       \/                \/           \/

On the most unix system, command line is heart of operating system. We can control everything in an operating system using command line. Normaly it's called shell, there were some application build in from kernel to do this as an interface from a user to a kernel. For example, sh, bash, csh, ksh, tcsh, etc. It's a command line interface (CLI) beside favorited most computer user is Graphical User Interface (GUI).

It's command line world. You can customize your command line skill to use a pattern. I will describe some standard pattern bellow :




? (Question mark)
First pattern is question mark is represent any single character. Test it with a word "test?", the result will be : test1, test2, testX, testa, test&, testy. Whatever it's a single character behind the '?'.

[wardi@localhost /]$ ls /dev/loop?
/dev/loop0  /dev/loop1  /dev/loop2  /dev/loop3  /dev/loop4  /dev/loop5  /dev/loop6  /dev/loop7

[wardi@localhost /]$ ls /dev/tty?5 /dev/ram?5
/dev/ram15  /dev/tty15  /dev/tty25  /dev/tty35  /dev/tty45  /dev/tty55

[wardi@localhost /]$ ls /dev/ram??
/dev/ram10  /dev/ram11  /dev/ram12  /dev/ram13  /dev/ram14  /dev/ram15


* (Asterisk)
The second pattern is asterisk. It's represent numbers of any character. One asterisk can represent a single any character and many characters. Many characters are any characters can be represent in one asterisk, include word, senteces, numbers, etc. Please check some example below :

[wardi@localhost /]$ ls /usr/bin/perl*
/usr/bin/perl  /usr/bin/perl5.10.1  /usr/bin/perlbug  /usr/bin/perldoc  /usr/bin/perlthanks

[wardi@localhost /]$ ls /proc/*info
/proc/buddyinfo  /proc/cpuinfo  /proc/meminfo  /proc/pagetypeinfo  /proc/slabinfo  /proc/vmallocinfo  /proc/zoneinfo

[wardi@localhost /]$ ls /dev/*1
/dev/hidraw1  /dev/lp1   /dev/ram11  /dev/sg1   /dev/tty11  /dev/tty31  /dev/tty51  /dev/ttyS1    /dev/vcs1
/dev/loop1    /dev/ram1  /dev/sda1   /dev/tty1  /dev/tty21  /dev/tty41  /dev/tty61  /dev/usbmon1  /dev/vcsa1


[] (Square bracket)
The third pattern is square bracket. This pattern will qualify single character inside the bracket. Then catch out the match character inside words or any string. Check it the results of matching character inside the square bracket bellow :

[wardi@localhost /]$ ls /dev/tty[1,2,3]
/dev/tty1  /dev/tty2  /dev/tty3

It's no problem for spesify range, example range from 1 to 3 inside a string :

[wardi@localhost /]$ ls /dev/tty[1-3]
/dev/tty1  /dev/tty2  /dev/tty3


We can combine two step before in a single command :

[wardi@localhost /]$ ls /dev/tty[5,S,6][1-3]
/dev/tty51  /dev/tty52  /dev/tty53  /dev/tty61  /dev/tty62  /dev/tty63  /dev/ttyS1  /dev/ttyS2  /dev/ttyS3


{} (Curly bracket)
This wildcard will qualify strings inside the curly bracket, and matching it then bring result the match string. Please check it below :

[wardi@localhost /]$ cp -v /proc/{meminfo,mdstat,swaps} $HOME/Documents/
`/proc/meminfo' -> `/home/wardi/Documents/meminfo'
`/proc/mdstat' -> `/home/wardi/Documents/mdstat'
`/proc/swaps' -> `/home/wardi/Documents/swaps'

Combine curly bracket with square bracket in one command :

[wardi@localhost /]$ ls -l /dev/{tty[1-3],crash,???[4-5]}
crw-rw----. 1 root root 10, 62 Nov  9 00:01 /dev/crash
brw-rw----. 1 root disk  1,  4 Nov  9 00:01 /dev/ram4
brw-rw----. 1 root disk  1,  5 Nov  9 00:01 /dev/ram5
crw--w----. 1 root tty   4,  1 Nov  9 00:01 /dev/tty1
crw-------. 1 root root  4,  2 Nov  9 00:01 /dev/tty2
crw-------. 1 root root  4,  3 Nov  9 00:01 /dev/tty3
crw-------. 1 root root  4,  4 Nov  9 00:01 /dev/tty4
crw-------. 1 root root  4,  5 Nov  9 00:01 /dev/tty5
crw-rw----. 1 vcsa tty   7,  4 Nov  9 00:01 /dev/vcs4
crw-rw----. 1 vcsa tty   7,  5 Nov  9 00:01 /dev/vcs5


Combine all of it : 

[wardi@localhost /]$ ls /proc/[1-2][1-2][1-2][1-2]/mount*
/proc/2112/mountinfo  /proc/2112/mounts  /proc/2112/mountstats

[wardi@localhost /]$ ls -l /proc/{*stat*,*info*,*list*}
-r--r--r--. 1 root root 0 Nov  9 01:52 /proc/buddyinfo
-r--r--r--. 1 root root 0 Nov  9 01:52 /proc/cpuinfo
-r--r--r--. 1 root root 0 Nov  9 01:52 /proc/diskstats
-r--r--r--. 1 root root 0 Nov  9 00:01 /proc/mdstat
-r--r--r--. 1 root root 0 Nov  9 00:01 /proc/meminfo
-r--r--r--. 1 root root 0 Nov  9 01:52 /proc/pagetypeinfo
-r--r--r--. 1 root root 0 Nov  9 01:52 /proc/schedstat
-rw-r--r--. 1 root root 0 Nov  9 01:52 /proc/slabinfo
-r--r--r--. 1 root root 0 Nov  9 00:01 /proc/stat
-r--r--r--. 1 root root 0 Nov  9 01:52 /proc/timer_list
-rw-r--r--. 1 root root 0 Nov  9 01:52 /proc/timer_stats
-r--------. 1 root root 0 Nov  9 01:52 /proc/vmallocinfo
-r--r--r--. 1 root root 0 Nov  9 00:01 /proc/vmstat
-r--r--r--. 1 root root 0 Nov  9 01:52 /proc/zoneinfo

[wardi@localhost /]$ ls /proc/[1-2][4-5]/{*stat*,*info*}
/proc/14/mountinfo   /proc/14/stat    /proc/15/mountinfo   /proc/15/stat    /proc/24/mountinfo   /proc/24/stat    /proc/25/mountinfo   /proc/25/stat
/proc/14/mountstats  /proc/14/statm   /proc/15/mountstats  /proc/15/statm   /proc/24/mountstats  /proc/24/statm   /proc/25/mountstats  /proc/25/statm
/proc/14/schedstat   /proc/14/status  /proc/15/schedstat   /proc/15/status  /proc/24/schedstat   /proc/24/status  /proc/25/schedstat   /proc/25/status

You can customize it as you want :)
#EOF

0 comments:

Article list :