Posted: . At: 3:44 PM. This was 6 years ago. Post ID: 12226
Page permalink. WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters.
These cookies expire two weeks after they are set.



Sponsored



Very useful ways to create wildcards with the Linux shell.


This is an example of a wildcard, using the [0-9] and [A-Z] wildcards to look for numbers and capital letters.

deusexmachina:Documents jason$ ls -hula [A-Z]rma[0-9]*
-rwxr-xr-x  1 jason  staff   1.7G  5 Mar 20:14 Arma3_x64_2017_11_13_21_50_20_373.avi
-rwxrwxrwx  1 jason  staff   1.2G 28 Jun 14:04 Arma3_x64_2018_01_04_15_29_53_844.avi
-rwxrwxrwx  1 jason  staff   1.0G  4 Jan 16:13 Arma3_x64_2018_01_04_15_32_03_159.avi
-rwxrwxrwx  1 jason  staff   2.9G 27 Mar 14:30 Arma3_x64_2018_01_04_15_34_52_367.avi

This wildcard example will find all filenames that start with a number.

deusexmachina:Documents jason$ ls -ulha [0-9]*
-rw-r--r--@ 1 jason  staff   163K 20 Feb 12:57 1514877771595.jpg
-rw-r--r--@ 1 jason  staff    51K 18 Jun 11:10 93bffab7a44bc518799a2a68711ad790.jpg

Find all filenames with the current year in the filename.

deusexmachina:Pictures jason$ ls | grep $(eval date +%Y)*
vlcsnap-2018-02-04-22h34m33s886.png
vlcsnap-2018-02-04-22h35m14s143.png

Another example, this is more complex.

deusexmachina:Pictures jason$ ls | grep [a-z]-$(eval date +%Y)-[0-9]*
vlcsnap-2018-02-04-22h34m33s886.png
vlcsnap-2018-02-04-22h35m14s143.png

Using $(eval date +%Y) instead of `date +%Y` is the proper way to run a command within a one-liner like this.

Listing all files that start with letters and end with a number in the filename.

deusexmachina:Desktop Pictures jason$ ls | grep "[a-z] [0-9]"
Color Burst 1.jpg
Color Burst 2.jpg
Color Burst 3.jpg
El Capitan 2.jpg
Sierra 2.jpg
Yosemite 2.jpg
Yosemite 3.jpg
Yosemite 4.jpg
Yosemite 5.jpg

Listing all filenames that start with the letters E to F.

deusexmachina:Desktop Pictures jason$ ls -hula [E-F]*
-rw-r--r--  1 root  wheel   4.2M  2 Jan 12:19 Earth Horizon.jpg
-rw-r--r--  1 root  wheel   3.8M  2 Jan 12:19 Earth and Moon.jpg
-rw-r--r--  1 root  wheel    11M  2 Jan 12:19 El Capitan 2.jpg
-rw-r--r--  1 root  wheel    12M  2 Jan 12:19 El Capitan.jpg
-rw-r--r--  1 root  wheel   8.4M  2 Jan 12:18 Elephant.jpg
-rw-r--r--  1 root  wheel   7.8M  2 Jan 12:19 Floating Ice.jpg
-rw-r--r--  1 root  wheel   4.2M  2 Jan 12:19 Foggy Forest.jpg
-rw-r--r--  1 root  wheel   7.7M  2 Jan 12:18 Foxtail Barley.jpg

That shows how easy the [E-F] range in square brackets is to use in bash. This can be very useful on a Linux or UNIX machine.


Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.