Use of touch and vi?

touching the file first confirms that you actually have the ability to create the file, rather than wasting time in an editor only to find out that the filesystem is read-only or some other problem.


Apart from the given answers, one advantage of touch is that any other user/terminal editing the same file while you touched it , will receive a warning when they try to save any changes.

WARNING: The file has been changed since reading it!!!
Do you really want to write to it (y/n)?

This would alert them even though you have not made any changes per se and only touched the file.


Apart from the accepted answer:

It is worth noting that touch is used to update file timestamps. If you use touch on a file that exists, it will then update the files timestamp to the current date and time. If the file does not exist, it creates an empty file with the current date and time as the timestamp.

vi, on the other hand, does not create a new file unless you write to it.

For example, if I typed vi test.txt, typed some notes, then typed :q!; test.txt would not exist.

Tags:

Vi

Shell

Touch