The use of the -f option for `touch`

coreutilstouch

From man touch:

-f     (ignored)

But I don't get what is meant by ignored.

I've tried following:

$ ls -l file
-rw-rw-r-- 1 pandya pandya 0 Mar 20 16:17 file

$ touch -f file
$ ls -l file
-rw-rw-r-- 1 pandya pandya 0 Mar 20 16:18 file

And noticed that it changes timestamps in spite of -f.

So, I want to know what -f stands for, or what it does.

Best Answer

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.

Related Question