Tiny Deathstars of Foulness

You search for items in macOS using compound conditions in a number of ways. One way is with awk. Here, we’re going to grab the output of a simple ls command. That gets piped into an awk statement. Then we’re going to look at the expression to evaluate. Basically, we’re going to say anything that contains com. as well as apple and .plist. Because it’s ls, we’re looking for names of files that match those patterns. Each pattern is listed in brackets. And then there’s the {print} to lay out the action of printing to the files that match the pattern to the screen:

ls |awk '/[com.][apple][.plist]/ {print}'

Note: I know you’re not supposed to use ls in scripts. Don’t care. If it were dates and such, I’d of used stat…

March 14th, 2017

Posted In: Mac OS X, Mac OS X Server

Tags: , , , ,

  • MikeH UI


    I love your nuggets of knowledge that you continue to offer, but this one had me stumped all morning when I should have been getting other things done. (Good thing it’s spring break!) I could not get your example to work reliably until I reworked it:

    ls | awk ‘/(com).(apple).*(plist)/ {print}’

    or, if I only wanted those files that ended in “plist”:

    ls | awk ‘/(com).(apple).*(plist)$/ {print}’

    It’s not perfect – I still have some minor variation from simply grepping over ls (which keeps pulling in a file from a subdirectory; that may be ls’ problem). It’s a lot closer than the listed example, which was matching seemingly random things like QuickTime Preferences and Correct me if I’m wrong, but in awk regular expressions, [brackets] instructs awk to match any character found within the brackets, whereas (parentheses) instructs awk to find this group of characters as written, and in that order?

    Anyway, whether I’m on the right track or not, I still learned a lot from this post: more about awk, more about regular expressions, and a whole lot more about the pain in the neck that is debugging regular expressions!