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).
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.
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.
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!
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.
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 "$@"
}
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
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.
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.