3

For some reason, I chose Macbook as my computer to managing remote Linux servers.

Sometimes I will encounter troubles with those utilities which have the same name but behave differently between these two operating systems.

Such as I used find ./ -name 'vim*' -exec stat -c "%y %n" {} \; well to find files and show their info nicely like:

2015-01-08 15:14:30.000000000 +0800 ./vimrc

But it won't work on my OSX. After reading the manual, I find I must use other flags like -f "%t%Sm %N" to get a result:

Mar  5 18:22:00 2015 ./vimrc

This sort of stuff really bothers me.

In my opinion, I think the Linux version of gadgets similar to stat is better, because it has more developer contribute to it, and keeps updating. And most users of OSX won't use these gadgets, they use the GUI. Thus many of its gadgets compare to Linux are antique.

I'd really want to treat the Linux version of usage as a standard, and left the OSX version along (don't touch them). But there is an annoying voice in my brain said this isn't right, it says "you should face your problem rather than evade it", "Learn them all", it said.

How should I deal with utilities that have the same name but different behavior in Unix and Linux?

migrated from programmers.stackexchange.com Mar 5 '15 at 19:45

This question came from our site for professionals, academics, and students working within the systems development life cycle.

3

Unix has a complex history. Over time, the many variants have accumulated many different commands and options.

There is a standard that most Unix-like systems, or rather a hierarchy of standards: POSIX, and POSIX+XSI (formerly known as Single Unix — Single Unix is now POSIX with the XSI parts marked as optional). You can count on anything Unix-like and not antique or embedded implementing POSIX-2004 a.k.a. Single Unix v3 a.k.a. Open Group Base Specification Issue 6; the newer version (POSIX-2008 = SUSv4 = Issue 7) isn't fully supported everywhere even today.

If you're only interested in OSX (and FreeBSD, which is where most OSX utilities are from) and non-embedded Linux, you can count on more commands and options: both are have more features than average. You need to check the manual to see what both OSes support.

Non-embedded Linux systems (and some higher-end embedded Linux systems), as well as Cygwin, come with GNU utilities. GNU utilities usually have many more convenient features than the rest of the competition.

The stat command is an example where POSIX says nothing, OSX has a certain syntax, and GNU has similar functionality but with a different syntax.

You can install GNU utilities on most Unix variants. For OSX, see https://apple.stackexchange.com/questions/69223/how-to-replace-mac-os-x-utilities-with-gnu-core-utilities.

Whether to stick to the common core or install the same versions everywhere is a personal decision. If you have control over all the machines that you use, just ensure that they all have all the utilities you want. If you write code that runs on machines where you aren't always able to install everything you want, then you need to stick to the portable stuff.

4

It's not Unix vs. Linux, but rather BSD vs. GNU.

Both families are available as source code and can be installed on either OS. Just googling "GNU for Mac" found several pages with instructions on using Homebrew, or other OSS package manager for mac, to replace the BSD tools with their GNU version.

About how to keep sanity when working with both sets of tools:

Most differences that I've seen fall on two camps: more options and more parsing flexibility.

The "more options" is what you refer in the question: GNU tools were created using the existing BSD and System V tools as examples, so the authors had the chance to start from scratch and add a few extra options, especially on fancy display options. Some of them are common for several tools. For example, many commands accept an -h or -H option to show "human readable units", like Kilo- Mega- Giga-, etc.

The parsing flexibility comes because most commands use the getopt library, which, in the GNU version, is more modern and capable. The most obvious consequence is that while most commands are documented as:

command [options] file file...

if they use the GNU getopt library, the options can be interspersed with the files. For example, in GNU you can use:

ls -lt fileA fileB

or

ls -l fileA -t fileB

and get the same result, because the -l and -t can be present anywhere in the command line. In BSD systems, the second form gives an error, or might try to find a file called -t.

With all this in mind, if you find yourself in a MacOS command line where you haven't installed GNU tools, just remember to put all options just after the command name, and don't expect it to have too many display format options.

  • You save my life! – Zen Mar 5 '15 at 15:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.