How to check version of GNU or BSD rm?

The GNU version rmhas the cool -I flag. From 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 do not:

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

Sometimes people have coreutils(version of GNU) installed on a Mac, and sometimes not. Is there any way to detect this command line flag before continuing? I would like to have something like this in my bash_profile:

if [ has_gnu_rm_version ]; then
    alias rm="rm -I"
fi
+5
source share
5 answers

I would say we are testing the output rm -Iin a temporary 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
+3
source

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

if $? 0, these are coreutils

+6
source

. , //, . - ?

To understand what I mean, see Ryan Tomayko Shell Haters discusses . It also has a very well organized page with links to descriptions of shell functions and POSIX utilities . Here rm , for example.

+5
source

You can always ask rm your version with --versionand check if it says gnu or coreutils:

rm --version 2>&1 | grep -i gnu &> /dev/null
[ $? -eq 0 ] && alias rm="rm -I"
+4
source

how about something like that?

#!/bin/bash
rm -I &> /dev/null
if [ "$?" == "0" ]; then
    echo coreutils detected
else
    echo bsd version detected
fi
0
source

All Articles