55

Is there a benefit of creating a file with touch prior to edit.. like:

touch foo
vi foo

versus getting it to editor straight-away? Like:

vi foo

I see quite a few tutorials using the former (touch then vi).

11 Answers 11

111

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.

  • 6
    And touch allows you to do chmod +x afterwards, before editing, which would make sense if you are writing a #!/bin/bash shell script. – Aaron McDaid Jun 3 '16 at 10:02
  • 11
    Alternatively, you can just do :w first thing after starting vi though. (and :chmod +x % if you want to make it executable) – Stéphane Chazelas Jun 3 '16 at 10:36
  • 26
    touch may fail where vi's :w! would have succeeded though (for instance if file exists but you're not its owner and don't have write access to it, while you have write access to the current directory). Conversely, touch may succeed but vi's :w or :w! may fail if you're owner of the file, but don't have write access to it nor to the current directory (you'd be able to work around it with :!chmod +w % (some vi implementations like vim can do that automatically upon :w!)). – Stéphane Chazelas Jun 3 '16 at 10:51
  • makes a lot of sense now! – Fawix Jun 3 '16 at 13:02
  • 4
    Vim / vi very clearly state if you open a file that you do not have permission to write to: "foo" [readonly]. And again when entering insert mode: Warning: Changing a readonly file. – DevSolar Jun 6 '16 at 9:44
25

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.

  • good point, so it's essentially a good way to discover if people is using/editing the same file and warn them at the same time. I see a clear benefit of that in a multi-tenant environment! – Fawix Jun 3 '16 at 13:12
  • @Fawix then you should update this answer Aswell – Kiwy Jun 3 '16 at 18:16
19

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.

  • What about the benefit described in the accepted answer? – JBentley Jun 5 '16 at 11:38
8

Without touch, a new file won't exist until you tell vi to write it.

Consider a multi-user system (perhaps you're on an network-mounted filesystem shared by many systems each with many users). Running touch will ensure you have the file (and that you can write to it) and even updates the timestamp. Another user wanting to create such a file will see that you own it. If it already exists and another user wants to delete or replace it, they'll see that it was recently modified and perhaps think twice.

4

There is no benefit to touching first; vi will create the file if it does not exist.

The accepted answer says it checks whether you can write there before wasting time in an editor. True, but now you'll be wasting time typing touch every time. Not being able to write somewhere is fairly exceptional compared to how often it will just work (as long as you remember sudo for files outside your home directory or /tmp, or are logged in as root).

Just open up the editor and do what you want, then try to save the file. If it doesn't work, even with :w!, save it elsewhere (:w ~/asdf) and fix the problem. Once it's fixed, you can copy the file contents from the temporary file to the original: cat ~/asdf > /mnt/example.txt && rm ~/asdf. The reason we use cat instead of mv or cp is to use the destination's permissions and other attributes.

Moreover, for some more advanced command line usage, you could background vi with Ctrl+Z while you fix the problem (or use :suspend, or :sus), and finally fg it again to run the write command.

Edit: post improved in response to /u/G-Man's comments. Thanks!

  • 3
    There are some security issues here: (1) If the file /mnt/example.txt is not supposed to be world-readable, but your umask is set to something permissive like 22, then /tmp/asdf will be world-readable.  If there are other people on the system, they may be able to read the temporary copy of the file. (2) The file /mnt/example.txt is probably not supposed to be world-writable, but, if there are malicious people on the system, they may be able to read the temporary copy of the file and replace it with a modified version before you move/copy it back to the right location.  … (Cont’d) – G-Man Jun 5 '16 at 12:29
  • 3
    (Cont’d) … (Setting the sticky bit on /tmp may prevent this.) (3) The command mv /tmp/asdf /mnt/example.txt will totally destroy the current /mnt/example.txt and replace it with /tmp/asdf. You want to replace the content of /mnt/example.txt with the content of /tmp/asdf. By using mv, you set up /mnt/example.txt to have the same attributes as /tmp/asdf — probably owned by you and world-readable. It might be better to cp /tmp/asdf /mnt/example.txt or even cat /tmp/asdf > /mnt/example.txt. … P.S. (4) You can suspend vi by typing :suspend (or :sus, for short). – G-Man Jun 5 '16 at 12:29
  • @G-Man Those are some good points. I'm not particularly scared of someone stealing a file during the 5 minutes it's in /tmp, but there is no excuse for not writing to ~/ instead. I also hadn't thought of that it would replace the attributes. I'll edit my post, thanks :) – Luc Jun 5 '16 at 15:45
3

There's no benefit. vi will create a file if doesn't exist.

1

vi is a visual text editor (vi = visual) It's visual compared to "ed" anyway, which just lets you see and change one line of text at a time.

The touch command updates the timestamp on an existing file, or creates a new file if the file didn't already exist. It's good for testing things that are highly dependent on timestamps.

Now if your file is a text file, or doesn't yet exist, opening it with vi, then issuing the command :wq to vi, would have the same result as touching that file. That's the only way the two commands are similar at all.

1

Specifically for use with vi, there is no need to create the file before you edit it: vi can be used to create and save a new file. However, there are calling contexts where the file needs to exist. For example, on my system (OS X) I can launch an appropriate GUI editor (determined by file type) like this:

open foo.txt

This would open foo.txt in TextEdit, or in emacs, or whatever I specified as my editor of choice for text files, and detach the process so that I get my prompt back immediately. (open bar.py might open it in IDLE, the python editor; etc.) Or I can explicitly request emacs:

open -a emacs foo.txt

But open requires the file to exist already, otherwise it raises an error. So I had to define emacs to be the following shell function, which allows me to write emacs foo to launch the emacs GUI even if foo does not exist.

function emacs () 
{ 
    if [ -n "$1" -a ! -e "$1" ]; then
        /usr/bin/touch "$1";
    fi;
    open -a emacs "$@"
}
0

vi is used to edit a file as user, while touch can set the timestamp on it and is mostly used in scripts and such.

Another way to create a file is:

>newfile.txt
  • 1
    This is unreliable because it's not (afaik) specified in the POSIX spec. For example, In zsh, it's identical to cat >newfile.txt and needs you to press Ctrl+D. – Adam Katz Jun 3 '16 at 22:05
  • @AdamKatz good point; I updated the answer, thanks for the advice. – James Youngman Jun 5 '16 at 10:56
0

By man page of touch his primary job is to change file timestamps.
Ofc is also creating the file with current timestamp and then you can edit the file.

VI is text editor that do what it says edit text open,save,edit file etc.

All is flavor of of user and habbit: touch then vi or vi file.txt same thing different colour.

0

touch command changes the date and time of a file with current time-stamp.
if file is does not exist, it creates new file with date and time.

vi editor is used to edit files if file is not exist it creates new file unless if we do not save the file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.