5

The GNU version of rm has a cool -I flag. From the manpage:

-I     prompt once before removing more than three files, or when removing recursively.   Less
          intrusive than -i, while still giving protection against most mistakes

Macs don't:

$ rm -I scratch
rm: illegal option -- I
usage: rm [-f | -i] [-dPRrvW] file ...
   unlink file

Sometimes people have coreutils (the GNU version) installed on Macs and sometimes they don't. Is there a way to detect this command line flag before proceeding? I'd like to have something like this in my bash_profile:

if [ has_gnu_rm_version ]; then
    alias rm="rm -I"
fi
Telemachus
  • 19,459
  • 7
  • 57
  • 79
Kevin Burke
  • 61,194
  • 76
  • 188
  • 305
  • I changed the title since I think the real question is "How can I best check for which of two versions of a core tool I have?" It isn't really about Mac versus Linux (closer maybe is BSD versus Linux, but even then, that's not your *real* question, I think). – Telemachus Jul 26 '11 at 19:49

5 Answers5

11

strings /bin/rm | grep -q 'GNU coreutils'

if $? is 0, it is coreutils

frankc
  • 11,290
  • 4
  • 32
  • 49
5

I would recommend not starting down this road at all. Target your scripts to be as portable as possible, and only rely on flags/options/behaviors you can count on. Shell scripting is hard enough - why add more room for error?

To get a sense of the kind of thing I have in mind, check out Ryan Tomayko's Shell Haters talk. He also has a very well-organized page with links to POSIX descriptions of shell features and utilities. Here's rm, for example.

Telemachus
  • 19,459
  • 7
  • 57
  • 79
  • Telemachus: I'm trying to put this in a `.bash_profile` I sync across my accounts on multiple computers. It'd be nice to use the `-I` flag if it exists. – Kevin Burke Jul 26 '11 at 18:34
  • 4
    @Kevin - in that case, my last bit of advice: Don't alias `rm` to anything. Use `rmi` or something else instead. (See comments here for some discussion: http://superuser.com/questions/31171/undo-the-linux-trash-command/31172#31172) – Telemachus Jul 26 '11 at 18:41
  • 1
    Excellent advice to not alias rm: you don't want to get in the habit of expecting to see a prompt, and then be on a foreign system and accidentally delete the wrong file. – glenn jackman Jul 26 '11 at 18:59
4

I'd say test the output of rm -I on a temp file, if it passes then use the alias

touch /tmp/my_core_util_check

if rm -I /tmp/my_core_util_check > /dev/null 2>&1 ; then
    alias rm="rm -I"
else
    rm /tmp/my_core_util_check;
fi
CharlesB
  • 86,532
  • 28
  • 194
  • 218
4

You could always ask rm its version with --version and check to see if it says gnu or coreutils like this:

rm --version 2>&1 | grep -i gnu &> /dev/null
[ $? -eq 0 ] && alias rm="rm -I"
Corey Henderson
  • 7,239
  • 1
  • 39
  • 43
0

how about something like this?

#!/bin/bash
rm -I &> /dev/null
if [ "$?" == "0" ]; then
    echo coreutils detected
else
    echo bsd version detected
fi
Oren Mazor
  • 4,437
  • 2
  • 29
  • 28
  • Oren: I ran this on a Mac with both versions of `rm`, and the script thought both versions were the BSD version. – Kevin Burke Jul 26 '11 at 18:25
  • @Kevin The GNU tools are often installed with a 'g' prefix on Macs, in order to avoid stepping on tools written for the BSD versions. Try `grm` maybe, though see my answer above. – Telemachus Jul 26 '11 at 18:33
  • 1
    Also, when you say "The script thought both versions...", how did you run this script against "both versions"? Just calling `rm` will pick up whichever one is earlier in your `PATH` (assuming they're both called `rm`. – Telemachus Jul 26 '11 at 18:48
  • Telemachus makes a good point. you could technically have both installed. perhaps address 'rm' directly by the path coreutils should use? – Oren Mazor Jul 26 '11 at 18:59
  • I might just be failing because 'rm -I' without any arguments will return a non-zero value in both cases. – Kevin S Jul 28 '11 at 05:00