What is the use of the -f option for `touch`?

For GNU utilities, the full documentation is in the info page, where you can read:

-f
Ignored; for compatibility with BSD versions of `touch'.

See historic BSD man pages for touch, where -f was to force the touch.

If you look at the source of those old BSDs, there was no utimes() system call, so touch would open the file in read+write mode, read one byte, seek back and write it again so as to update the last access and last modification time.

Obviously, you needed both read and write permissions (touch would avoid trying to do that if access(W_OK|R_OK) returned false). -f tried to work around that by temporarily changing the permissions temporarily to 0666!

0666 means read and write permission to everybody. It had to be that as otherwise (like with a more restrictive permission such as 0600 that still would have permitted the touch) that could mean during that short window, processes that would otherwise have read or write permission to the file couldn't any more, breaking functionality.

That means however that processes that would not otherwise have access to the file now have a short opportunity to open it, breaking security.

That's not a very sensible thing to do. Modern touch implementations don't do that. Since then, the utime() system call has been introduced, allowing changing modification and access time separately without having to mingle with the content of the files (which means it also works with non-regular files) and only needs write access for that.

GNU touch still doesn't fail if passed the -f option, but just ignores the flag. That way, scripts written for those old versions of BSD don't fail when ported to GNU systems. Not much relevant nowadays.


-f does nothing. It's kept for historic compatibility (with BSD touch according to info touch), so that applications that expect it to exist don't pass it and get back an error message saying it doesn't exist. Assuming that this is GNU coreutils, you can see in the source that this just does a break out of the option processing switch without doing anything.

As an ignored option, -f is present from the very first version of the GNU touch command added in 1992 (see diff). It seems that, at least in FreeBSD v9, -f "attempt[s] to force the update, even if the file permissions do not currently permit it" (as found by Sukminder, thanks).


Whenever you see "option X is ignored" in --help output or a manpage, that means: the program accepts option X — you do not get a syntax error — but it doesn't have any effect. The program does the same thing it would have done in the absence of the option.

As the other answers demonstrate, this is done for backward compatibility. An option used to have some effect, whatever it did is no longer useful, and doing the same thing regardless of the option is the right compatibility behavior.