Bash Extended Globbing
Wildcards in bash are referred to as pathname expansion. Pathname expansion is also sometimes referred to as globbing. Pathname expansion "expands" the "*", "?", and "[...]" syntaxes when you type them as part of a command, for example:
$ ls *.jpg # List all JPEG files $ ls ?.jpg # List JPEG files with 1 char names (eg a.jpg, 1.jpg) $ rm [A-Z]*.jpg # Remove JPEG files that start with a capital letterA subtle point about pathname expansion that is not often understood is that it is done by bash and not by the operating system or by the program that is being run. The program never sees the wildcards, bash substitutes the expansion into the command line before running the program. This is rarely important except when you're writing code and calling exec() and friends: if you don't execute the program via bash any wildcards in the command line that you pass to exec() won't get expanded.
But these are not the only forms of wildcards supported by bash. The other forms are referred to as extended globbing and you must enable them before you can use them:
$ shopt -s extglobExtended globbing as described by the bash man page:
?(pattern-list) Matches zero or one occurrence of the given patterns *(pattern-list) Matches zero or more occurrences of the given patterns +(pattern-list) Matches one or more occurrences of the given patterns @(pattern-list) Matches one of the given patterns !(pattern-list) Matches anything except one of the given patternsHere a pattern-list is a list of items separated by a vertical bar "|" (aka the pipe symbol). If you look at these you can see why the leading character was chosen as it matches what is used in regular expression syntax:
Bash Regular Expression ?(pattern-list) (...|...)? *(pattern-list) (...|...)* +(pattern-list) (...|...)+ @(pattern-list) (...|...) [@ not a RE syntax] !(pattern-list) "!" used as for negative assertions in RE syntaxWell, except for the "@" character you can see why...
For example, to list all the JPEG and GIF files that start with either "ab" or "def" you could do:
$ ls +(ab|def)*+(.jpg|.gif)Of course you could also do this without extended globbing:
# ls ab*.jpg ab*.gif def*.jpg def*.gif
To list all the files that match the regular expression "ab(2|3)+.jpg" you could do:
$ ls ab+(2|3).jpgNow that's something you can't do with regular globbing. Note: this matches files like ab2.jpg, ab3.jpg, ab2222.jpg, ab333.jpg, etc.
However, probably the most interesting extended globbing syntax is the "!(...)" syntax for matching everything except (ie not matching). But, be careful, this doesn't always do what you might expect. For example, let's list all the files that aren't JPEGs or GIFs. One's first thought might be something like this:
$ ls *!(.jpg|.gif) # wrong, Wrong, WRONGBut that doesn't work because the ".jpg" and the ".gif" of any file's name end up getting matched by the "*" and the null string at the end of the file name is the part that ends up not matching the "!(...)" pattern. The correct syntax is:
$ ls !(*.jpg|*.gif)
For a more complex negation example, let's go back to the first example and list all the files that aren't JPEG or GIF files and start with either "ab" or "def". This is actually quite simple, we just take the first example and nest it inside of "!(...)":
$ ls !(+(ab|def)*+(.jpg|.gif))Of course, like complex regular expressions, this will be completely incomprehensible 10 minutes after you write it.
Mitch Frazier is an Associate Editor for Linux Journal.
Practical Task Scheduling Deployment
July 20, 2016 12:00 pm CDT
One of the best things about the UNIX environment (aside from being stable and efficient) is the vast array of software tools available to help you do your job. Traditionally, a UNIX tool does only one thing, but does that one thing very well. For example, grep is very easy to use and can search vast amounts of data quickly. The find tool can find a particular file or files based on all kinds of criteria. It's pretty easy to string these tools together to build even more powerful tools, such as a tool that finds all of the .log files in the /home directory and searches each one for a particular entry. This erector-set mentality allows UNIX system administrators to seem to always have the right tool for the job.
Cron traditionally has been considered another such a tool for job scheduling, but is it enough? This webinar considers that very question. The first part builds on a previous Geek Guide, Beyond Cron, and briefly describes how to know when it might be time to consider upgrading your job scheduling infrastructure. The second part presents an actual planning and implementation framework.
Join Linux Journal's Mike Diehl and Pat Cameron of Help Systems.
Free to Linux Journal readers.Register Now!
- Stunnel Security for Oracle
- SourceClear Open
- SUSE LLC's SUSE Manager
- Murat Yener and Onur Dundar's Expert Android Studio (Wrox)
- My +1 Sword of Productivity
- Managing Linux Using Puppet
- Tech Tip: Really Simple HTTP Server with Python
- Non-Linux FOSS: Caffeine!
- Google's SwiftShader Released
- Doing for User Space What We Did for Kernel Space
With all the industry talk about the benefits of Linux on Power and all the performance advantages offered by its open architecture, you may be considering a move in that direction. If you are thinking about analytics, big data and cloud computing, you would be right to evaluate Power. The idea of using commodity x86 hardware and replacing it every three years is an outdated cost model. It doesn’t consider the total cost of ownership, and it doesn’t consider the advantage of real processing power, high-availability and multithreading like a demon.
This ebook takes a look at some of the practical applications of the Linux on Power platform and ways you might bring all the performance power of this open architecture to bear for your organization. There are no smoke and mirrors here—just hard, cold, empirical evidence provided by independent sources. I also consider some innovative ways Linux on Power will be used in the future.Get the Guide