![]() ![]() Wildcards are an incredibly powerful tool, which are often misunderstood. Why People Only Think They Understand Wildcards Then use the wildcard pattern r* as an argument to cp as follows:- $ cp r* copiesĬonfirm it has copied the files to the subdirectory:- $ ls copies First create a new empty directory called copies:- $ mkdir copies Say you wanted to make a copy of all the report files to a new subdirectory. The key thing to remember when using it is to ask: what is the minimum pattern that would uniquely identify what I’m looking for? A file called raptor999 would also match. So in the final example above, anything that starts with the letter ris enough to match – the asterisk matches all the remaining characters. The magic thing about the asterisk is it tries to match any number of characters (including none). In fact, it’s easier than that:- $ ls report* Let’s see why…įiltering the report files is easy with the asterisk wildcard:- $ ls report*.txt A version of it exists in Windows, too, but it’s nowhere near as powerful. This is the workhorse of wildcards, and is the one most people know about. Thankfully there’s another way: the * (asterisk) wildcard. To see why, imagine what would happen when the number of report files in the example directory reached 10: the filter would no longer match because it only matches a single character, and now the number is two digits long. Single-character wildcards are quite limiting, and in reality they’re not used terribly often. It has then found all the files in the current directory than match this pattern and listed them using ls. The pattern matches filenames composed of the string report followed by another single character, followed by the string. What has happened here? The shell has treated the ? character as a special character that means “match any character”. Report1.txt report2.txt report3.txt report4.txt report5.txt Now what if you want to do some common operation to just the report files? You can use the single wildcard character ? to specify all the report files like this:- $ ls report?.txt Suppose you have a directory with a lot of similarly-named files in it:- $ lsĪppendix.txt report1.txt report3.txt report5.txt Because just like the Joker or “wild card” in a pack of cards, a wildcard is a character that can act as a stand-in for any other character (or characters) in a file path. That’s patently incorrect, but probably forgivable in those pre-Google days (he didn’t look like a hardcore card player). Many, many years ago I remember a tutor in an introductory CS lecture telling the whole class that wildcards were nothing to do with cards. It’s a three-hour course covering everything you need to know to make you a command line pro – and it’s available free for a limited time only! What’s a Wildcard? The content of this post is now covered in some depth on my Udemy course Learn to be a macOS Command Line Ninja. Many casual command line users will have a passing familiarity with them but most people don’t really understand how they actually work. A very useful concept on the macOS (and Linux) command line is that of wildcards. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |