2

I'm learning linux bash on my mac. Is there an equivalent to this command in OSX?

locate --regex 'bin/(bz|gz|zip)'
  • Did you generate the locate database? If not, I would use find or mdfind. The command should be locate '/bin \(gz|zip\)' but I currently can't test to validate it. If I remember correctly the BSD version of locate doesn't have a regex option. – Allan Aug 28 '17 at 3:17
  • 1
    try locate / | egrep 'pattern'. But as mentioned, make sure the locate db is enabled ... egrep is "extended grep" which will support patterns with parentheses and pipes like that (without extra escaping) – 0942v8653 Aug 28 '17 at 13:07
  • @0942v8653 that worked! Thanks! locate /usr/bin/ | egrep '(bz|gz|zip)' – user3064085 Aug 28 '17 at 21:03
2

The macOS version of locate doesn’t have a regex option. You can use * and ? in the search pattern but that’s it.

Before you can use locate you will have to enable it (i. e. have the locate database built) using the following command—by default it is disabled:

sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.locate.plist
  • 1
    Thanks! I also updated the db with this command: sudo /usr/libexec/locate.updatedb – user3064085 Aug 28 '17 at 21:08
  • Yes, this is an alternative to enabling the LaunchDaemon. However, the latter has the advantage of being automatically repeated every Sunday at 3:15 pm which keeps the database current. – Tom E Aug 29 '17 at 7:35
0

The default version of locate on OS X/macOS does not support this, like Tom E. indicated.

Apple ships the BSD version of most of the command-line tools (formerly called "the BSD subsystem").

Therefore there are two answers to your question:

  1. You might construct any number of equivalent command oneliners that accomplish the same as the one command you quoted. For learners, the keywords there are not only regex, but also pipes, backticks and some more.

  2. But to consider your first line of context ("learning linux bash") the following answer may be more appropriate:
    Your example might be from a book like "The Linux Command Line: A Complete Introduction". That is problematic to apply word for word or command by command on OS X/macOS. Apple mostly ships BSD tools and (almost?) all of them in very outdated and insecure versions (fanatical GPL3-allergy on Apple's side). Linux ships mostly GNU-tools that might/will have often the same names but differing features/options compared to their BSD equivalents.

macOS can therefore have not only an equivalent, but the exact same command and syntax you gave as an example. But for that you have to install the corresponding GNU tools. In your case above findutils. That contains the locate command in the flavour used by your example.

This is generally recommendable if you want to learn "linux bash". Install all GNU tools you might need to follow the examples. This also includes a current bash. Apple ships the horrendously medieval version of

/bin/bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin16)
Copyright (C) 2007 Free Software Foundation, Inc.

Whereas GNU bash is much newer:

/usr/local/bin/bash --version
GNU bash, version 4.4.12(1)-release (x86_64-apple-darwin16.7.0)
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
  • If you want to learn macOS/BSD bash/command line you should get other guides or books.

  • If you want to stick to your current guides or really learn the GNU/Linux way, then you should install the GNU tools.

You can use package managers to do this on macOS, just like on other BSDs or linux.

Available package managers to simply your life are fink, macports and homebrew. More links to them and some discussion about their respective merits and detriments can be found here.

  • 2
    I'd advise against this path. If you want to learn linux on your mac, SSH into a linux box or use a virtual machine. MacOS is case preserving, but not case sensitive, which is a significant difference. Those GNU options are great, but if you are learning, the little differences will mess you up. Better to duplicate your work environment as closely as possible, save the mixing until you have a better grasp of POSIX – chiggsy Aug 28 '17 at 12:08
  • While I certainly will not advise against my own 'path' just given, chiggsy adds a nice variant, even if that is a bit farther away from "on my mac" of the OPs question. – LangLangC Aug 28 '17 at 14:43
  • What @chiggsy said. I am not a fan of mixing different flavors of the same tools on the same system (especially if you're new at this) - it introduces confusion and complexity. If you are looking for a learning environment, light up a VM (VirtualBox is great) and learn in that environment natively. The upside is that when you are done with that environment, you can trash it and light up a new different one (i.e. Ubuntu then Fedora) – Allan Aug 28 '17 at 14:43
  • 1
    @LangLangC you are correct, I am using "The Linux Command Line: A Complete Introduction". – user3064085 Aug 28 '17 at 21:30
  • 1
    I did install findutils with brew and I then had glocate. I just now finally figured out how to updatedb with gupdatedb. But now it doesn't seem to be seeing the path provided correctly glocate /usr/bin/ -r '(bz|gz|zip)' I guess that's something else to figure out later. I have 2 VM's, Ubuntu and CentOS. When things don't work I try it CentOS. I like trying to find out alternative commands to make it work since I use Linux and OSX for work. cat -A distros.txt (didn't work) cat -et distros.txt (did) – user3064085 Aug 28 '17 at 21:45
0

Instasll findutils

brew install findutils

This package contains gupdatedb and glocate commands, which are copies of linux GNU tools.

  • This does not seem to fix the issue. – haykam Feb 17 at 2:13
  • You should use it like glocate -r 'bin/(bz|gz|zip)'. And you should call gupdatedb first. – Dimitrius Feb 17 at 14:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .