Raspberry Pi Pico Tips and Tricks

Wednesday, 23 March 2016

Wildcards in Linux

The following post is a section of the book 'Just Enough Linux'.  The entire book can be downloaded in pdf format for free from Leanpub or you can read it online here.
Since this post is a snapshot in time. I recommend that you download a copy of the book which is updated frequently to improve and expand the content.
---------------------------------------

An important concept to grasp in Linux is that of using wildcards. In a sporting context a wildcard is something (or someone) that can be introduced to a game as a substitute. It may not have a strictly defined value, but could fill in for a range of standard objects.
Wildcards are a feature on the Linux command line (and other places) that makes the command line far more versatile than graphical file managers. Anyone who’s tried to use fancy combinations of shift / ctrl and mouse clicking while sorting in a file manager will attest to a degree of difficulty.
For example, if we have a directory with a large number files and sub-directories, and we need to move all the Python (files ending in .py) files, that have the word ‘pi’ somewhere in their names, from that large directory into another directory. This has the potential to be a time consuming task.
At the Linux command line that task is almost as easy to carry out as moving only one Python file, and it’s simple because of the wildcards. These are special characters that allow us to select file names that match certain patterns of characters. This helps us select a range of matching of files by typing just a few characters, and in most cases (IMHO) it’s easier than using a graphical file manager.
Here’s a list of the most commonly used wildcards :
Wildcard        Matches
   *            zero or more characters
   ?            exactly one character
[abcde]         exactly one of the characters listed
 [a-e]          exactly one character in the range specified
[!abcde]        any character that is not listed
 [!a-e]         any character that is not in the range specified
{pi,raspberry}  exactly one entire word from the options given
As well as being able to be applied in isolation, some of the real strengths of wildcards comes when using them in combination.

Examples

Zero or more characters

To list all Python files with ‘pi’ in their names;

Exactly one character

To list all files in the current directory with three character file extensions;

Exactly one of the characters listed

To list all files that have an extension that begins with ‘p’ or ‘j’;

Exactly one character in the range specified

To list all files that have a number in the file name;

Combination

To list all files that start with one of the three first or three last letters of the alphabet and end with an extension ‘jpg’ or ‘png’.


The post above (and heaps of other stuff) is in the book 'Just Enough Linux' that can be downloaded for free (or donate if you really want to :-)).

1 comment: