Use of touch and vi?












56















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).










share|improve this question





























    56















    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).










    share|improve this question



























      56












      56








      56


      5






      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).










      share|improve this question
















      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).







      shell vi touch






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 3 '16 at 10:13









      muru

      1




      1










      asked Jun 2 '16 at 21:48









      FawixFawix

      454614




      454614






















          11 Answers
          11






          active

          oldest

          votes


















          112














          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.






          share|improve this answer



















          • 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








          • 9





            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






          • 25





            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.






          share|improve this answer
























          • 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.






          share|improve this answer


























          • What about the benefit described in the accepted answer?

            – JBentley
            Jun 5 '16 at 11:38











          • @JBentely: thanks, I just forgot to update.

            – Peschke
            Jun 5 '16 at 18:23



















          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.






          share|improve this answer































            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!






            share|improve this answer





















            • 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.






            share|improve this answer

































              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.






              share|improve this answer































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





                share|improve this answer































                  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





                  share|improve this answer



















                  • 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.






                  share|improve this answer































                    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.






                    share|improve this answer























                      Your Answer








                      StackExchange.ready(function() {
                      var channelOptions = {
                      tags: "".split(" "),
                      id: "106"
                      };
                      initTagRenderer("".split(" "), "".split(" "), channelOptions);

                      StackExchange.using("externalEditor", function() {
                      // Have to fire editor after snippets, if snippets enabled
                      if (StackExchange.settings.snippets.snippetsEnabled) {
                      StackExchange.using("snippets", function() {
                      createEditor();
                      });
                      }
                      else {
                      createEditor();
                      }
                      });

                      function createEditor() {
                      StackExchange.prepareEditor({
                      heartbeatType: 'answer',
                      autoActivateHeartbeat: false,
                      convertImagesToLinks: false,
                      noModals: true,
                      showLowRepImageUploadWarning: true,
                      reputationToPostImages: null,
                      bindNavPrevention: true,
                      postfix: "",
                      imageUploader: {
                      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                      allowUrls: true
                      },
                      onDemand: true,
                      discardSelector: ".discard-answer"
                      ,immediatelyShowMarkdownHelp:true
                      });


                      }
                      });














                      draft saved

                      draft discarded


















                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f287316%2fuse-of-touch-and-vi%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown

























                      11 Answers
                      11






                      active

                      oldest

                      votes








                      11 Answers
                      11






                      active

                      oldest

                      votes









                      active

                      oldest

                      votes






                      active

                      oldest

                      votes









                      112














                      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.






                      share|improve this answer



















                      • 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








                      • 9





                        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






                      • 25





                        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


















                      112














                      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.






                      share|improve this answer



















                      • 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








                      • 9





                        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






                      • 25





                        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
















                      112












                      112








                      112







                      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.






                      share|improve this answer













                      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.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jun 2 '16 at 22:04









                      DopeGhotiDopeGhoti

                      45k55988




                      45k55988








                      • 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








                      • 9





                        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






                      • 25





                        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
















                      • 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








                      • 9





                        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






                      • 25





                        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










                      6




                      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







                      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






                      9




                      9





                      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





                      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




                      25




                      25





                      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





                      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





                      makes a lot of sense now!

                      – Fawix
                      Jun 3 '16 at 13:02




                      4




                      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







                      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.






                      share|improve this answer
























                      • 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
















                      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.






                      share|improve this answer
























                      • 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














                      25












                      25








                      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.






                      share|improve this answer













                      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.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Jun 3 '16 at 10:19









                      amisaxamisax

                      1,491515




                      1,491515













                      • 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



















                      • 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

















                      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





                      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





                      @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.






                      share|improve this answer


























                      • What about the benefit described in the accepted answer?

                        – JBentley
                        Jun 5 '16 at 11:38











                      • @JBentely: thanks, I just forgot to update.

                        – Peschke
                        Jun 5 '16 at 18:23
















                      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.






                      share|improve this answer


























                      • What about the benefit described in the accepted answer?

                        – JBentley
                        Jun 5 '16 at 11:38











                      • @JBentely: thanks, I just forgot to update.

                        – Peschke
                        Jun 5 '16 at 18:23














                      19












                      19








                      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.






                      share|improve this answer















                      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.







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Jun 5 '16 at 18:22

























                      answered Jun 2 '16 at 22:11









                      PeschkePeschke

                      2,558925




                      2,558925













                      • What about the benefit described in the accepted answer?

                        – JBentley
                        Jun 5 '16 at 11:38











                      • @JBentely: thanks, I just forgot to update.

                        – Peschke
                        Jun 5 '16 at 18:23



















                      • What about the benefit described in the accepted answer?

                        – JBentley
                        Jun 5 '16 at 11:38











                      • @JBentely: thanks, I just forgot to update.

                        – Peschke
                        Jun 5 '16 at 18:23

















                      What about the benefit described in the accepted answer?

                      – JBentley
                      Jun 5 '16 at 11:38





                      What about the benefit described in the accepted answer?

                      – JBentley
                      Jun 5 '16 at 11:38













                      @JBentely: thanks, I just forgot to update.

                      – Peschke
                      Jun 5 '16 at 18:23





                      @JBentely: thanks, I just forgot to update.

                      – Peschke
                      Jun 5 '16 at 18:23











                      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.






                      share|improve this answer




























                        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.






                        share|improve this answer


























                          8












                          8








                          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.






                          share|improve this answer













                          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.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Jun 3 '16 at 22:02









                          Adam KatzAdam Katz

                          2,2021121




                          2,2021121























                              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!






                              share|improve this answer





















                              • 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
















                              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!






                              share|improve this answer





















                              • 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














                              4












                              4








                              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!






                              share|improve this answer















                              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!







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited 5 mins ago









                              Solomon Ucko

                              1076




                              1076










                              answered Jun 4 '16 at 11:21









                              LucLuc

                              9361817




                              9361817








                              • 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 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




                              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





                              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




                              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





                              (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





                              @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.






                              share|improve this answer






























                                3














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






                                share|improve this answer




























                                  3












                                  3








                                  3







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






                                  share|improve this answer















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







                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Jun 2 '16 at 22:18

























                                  answered Jun 2 '16 at 22:03









                                  ronakgronakg

                                  1394




                                  1394























                                      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.






                                      share|improve this answer




























                                        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.






                                        share|improve this answer


























                                          1












                                          1








                                          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.






                                          share|improve this answer













                                          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.







                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered Jun 3 '16 at 10:26









                                          malyymalyy

                                          1,01767




                                          1,01767























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





                                              share|improve this answer




























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





                                                share|improve this answer


























                                                  1












                                                  1








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





                                                  share|improve this answer













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






                                                  share|improve this answer












                                                  share|improve this answer



                                                  share|improve this answer










                                                  answered Jun 5 '16 at 17:56









                                                  alexisalexis

                                                  4,06421526




                                                  4,06421526























                                                      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





                                                      share|improve this answer



















                                                      • 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














                                                      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





                                                      share|improve this answer



















                                                      • 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












                                                      0








                                                      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





                                                      share|improve this answer













                                                      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






                                                      share|improve this answer












                                                      share|improve this answer



                                                      share|improve this answer










                                                      answered Jun 3 '16 at 9:57









                                                      licklakelicklake

                                                      1062




                                                      1062








                                                      • 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














                                                      • 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








                                                      1




                                                      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





                                                      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





                                                      @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.






                                                      share|improve this answer




























                                                        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.






                                                        share|improve this answer


























                                                          0












                                                          0








                                                          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.






                                                          share|improve this answer













                                                          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.







                                                          share|improve this answer












                                                          share|improve this answer



                                                          share|improve this answer










                                                          answered Jun 4 '16 at 11:52









                                                          GiannakopoulosJGiannakopoulosJ

                                                          442213




                                                          442213























                                                              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.






                                                              share|improve this answer




























                                                                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.






                                                                share|improve this answer


























                                                                  0












                                                                  0








                                                                  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.






                                                                  share|improve this answer













                                                                  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.







                                                                  share|improve this answer












                                                                  share|improve this answer



                                                                  share|improve this answer










                                                                  answered Sep 3 '16 at 0:51









                                                                  PremrajPremraj

                                                                  1,07011017




                                                                  1,07011017






























                                                                      draft saved

                                                                      draft discarded




















































                                                                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                                                      • Please be sure to answer the question. Provide details and share your research!

                                                                      But avoid



                                                                      • Asking for help, clarification, or responding to other answers.

                                                                      • Making statements based on opinion; back them up with references or personal experience.


                                                                      To learn more, see our tips on writing great answers.




                                                                      draft saved


                                                                      draft discarded














                                                                      StackExchange.ready(
                                                                      function () {
                                                                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f287316%2fuse-of-touch-and-vi%23new-answer', 'question_page');
                                                                      }
                                                                      );

                                                                      Post as a guest















                                                                      Required, but never shown





















































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown

































                                                                      Required, but never shown














                                                                      Required, but never shown












                                                                      Required, but never shown







                                                                      Required, but never shown







                                                                      Popular posts from this blog

                                                                      サソリ

                                                                      広島県道265号伴広島線

                                                                      Accessing regular linux commands in Huawei's Dopra Linux