1

I learned that -i option is interactive mode and -f option is force model in rm command.

When I tried both options

rm -if test.txt

it did not ask me and just deleted it which means -f option overrode -i option.

Of course, I would not use options -i and -f at the same time in real life. But I wonder if there is a priority if two contradictory options are used at the same time.

I tried this in Ubuntu 22.04.

3
  • 2
    Try using rm -fi test.txt to see what happens. The options are read in order and the last option's functionality is what is enforced. And you might not plan on using this in real life but many paranoid people alias ls to ls -i and then might be caught by ls -f foo.
    – doneal24
    Commented Apr 29, 2023 at 0:22
  • 3
    ... the coreutils documentation (info coreutils 'rm invocation') notes under -f that it will "Ignore any previous ‘--interactive’ (‘-i’) option." Commented Apr 29, 2023 at 0:53
  • Worth pointing out that cp -f doesn't override -i: unix.stackexchange.com/questions/744420/…
    – paradroid
    Commented Apr 29, 2023 at 13:04

2 Answers 2

2

In general: no, Unix commands do not have a notion of flag priority.

From the Open Group Base Spec section on Utility Arguments

The use of conflicting mutually-exclusive arguments produces undefined results, unless a utility description specifies otherwise.

It's up to the command implicitly decide which one has priority or perhaps warn or error if conflicting flags are given.

As noted, in the case of rm, -f will override any previous -i.

As another example, if you ask tar to both create and archive -c and extract an archive -x, it'll complain that you're not making sense:

$ tar cxf test
tar: You may not specify more than one '-Acdtrux', '--delete' or  '--test-label' option
1

In addition to @ckhan's answer: the implementations of rm I worked with always considered the last given argument as final. That means:

rm -fi # will work interactively
rm -if # will work non-interactively
rm -ffifi # will work interactively

etc.. For instance, the AIX manpage (AIX 7.2) states:

If both the -f and -i flags are specified, the last one specified takes affect.

You must log in to answer this question.

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