Use of touch and vi?

Clash Royale CLAN TAG#URR8PPP
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
add a comment |
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
add a comment |
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
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
shell vi touch
edited Jun 3 '16 at 10:13
muru
1
1
asked Jun 2 '16 at 21:48
FawixFawix
454614
454614
add a comment |
add a comment |
11 Answers
11
active
oldest
votes
touching the file first confirms that you actually have the ability to create the file, rather than wasting time in an editor only to find out that the filesystem is read-only or some other problem.
6
Andtouchallows you to dochmod +xafterwards, before editing, which would make sense if you are writing a#!/bin/bashshell script.
– Aaron McDaid
Jun 3 '16 at 10:02
11
Alternatively, you can just do:wfirst thing after startingvithough. (and:chmod +x %if you want to make it executable)
– Stéphane Chazelas
Jun 3 '16 at 10:36
26
touchmay fail wherevi's:w!would have succeeded though (for instance iffileexists but you're not its owner and don't have write access to it, while you have write access to the current directory). Conversely,touchmay succeed butvi's:wor: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 %(someviimplementations likevimcan 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
|
show 1 more comment
Apart from the given answers, one advantage of touch is that any other user/terminal editing the same file while you touched it , will receive a warning when they try to save any changes.
WARNING: The file has been changed since reading it!!!
Do you really want to write to it (y/n)?
This would alert them even though you have not made any changes per se and only touched the file.
good point, so it's essentially a good way to discover if people is using/editing the same file and warn them at the same time. I see a clear benefit of that in a multi-tenant environment!
– Fawix
Jun 3 '16 at 13:12
@Fawix then you should update this answer Aswell
– Kiwy
Jun 3 '16 at 18:16
add a comment |
Apart from the accepted answer:
It is worth noting that touch is used to update file timestamps. If you use touch on a file that exists, it will then update the files timestamp to the current date and time. If the file does not exist, it creates an empty file with the current date and time as the timestamp.
vi, on the other hand, does not create a new file unless you write to it.
For example, if I typed vi test.txt, typed some notes, then typed :q!; test.txt would not exist.
What about the benefit described in the accepted answer?
– JBentley
Jun 5 '16 at 11:38
@JBentely: thanks, I just forgot to update.
– Peschke
Jun 5 '16 at 18:23
add a comment |
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.
add a comment |
There is no benefit to touching first; vi will create the file if it does not exist.
The accepted answer says it checks whether you can write there before wasting time in an editor. True, but now you'll be wasting time typing touch every time. Not being able to write somewhere is fairly exceptional compared to how often it will just work (as long as you remember sudo for files outside your home directory or /tmp, or are logged in as root).
Just open up the editor and do what you want, then try to save the file. If it doesn't work, even with :w!, save it elsewhere (:w ~/asdf) and fix the problem. Once it's fixed, you can copy the file contents from the temporary file to the original: cat ~/asdf > /mnt/example.txt && rm ~/asdf. The reason we use cat instead of mv or cp is to use the destination's permissions and other attributes.
Moreover, for some more advanced command line usage, you could background vi with Ctrl+Z while you fix the problem (or use :suspend, or :sus), and finally fg it again to run the write command.
Edit: post improved in response to /u/G-Man's comments. Thanks!
3
There are some security issues here: (1) If the file/mnt/example.txtis not supposed to be world-readable, but your umask is set to something permissive like 22, then/tmp/asdfwill 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.txtis 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/tmpmay prevent this.) (3) The commandmv /tmp/asdf /mnt/example.txtwill totally destroy the current/mnt/example.txtand replace it with/tmp/asdf. You want to replace the content of/mnt/example.txtwith the content of/tmp/asdf. By usingmv, you set up/mnt/example.txtto have the same attributes as/tmp/asdf— probably owned by you and world-readable. It might be better tocp /tmp/asdf /mnt/example.txtor evencat /tmp/asdf > /mnt/example.txt. … P.S. (4) You can suspendviby 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
add a comment |
There's no benefit. vi will create a file if doesn't exist.
add a comment |
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.
add a comment |
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 "$@"
add a comment |
vi is used to edit a file as user, while touch can set the timestamp on it and is mostly used in scripts and such.
Another way to create a file is:
>newfile.txt
1
This is unreliable because it's not (afaik) specified in the POSIX spec. For example, Inzsh, it's identical tocat >newfile.txtand 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
add a comment |
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.
add a comment |
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.
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
touching the file first confirms that you actually have the ability to create the file, rather than wasting time in an editor only to find out that the filesystem is read-only or some other problem.
6
Andtouchallows you to dochmod +xafterwards, before editing, which would make sense if you are writing a#!/bin/bashshell script.
– Aaron McDaid
Jun 3 '16 at 10:02
11
Alternatively, you can just do:wfirst thing after startingvithough. (and:chmod +x %if you want to make it executable)
– Stéphane Chazelas
Jun 3 '16 at 10:36
26
touchmay fail wherevi's:w!would have succeeded though (for instance iffileexists but you're not its owner and don't have write access to it, while you have write access to the current directory). Conversely,touchmay succeed butvi's:wor: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 %(someviimplementations likevimcan 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
|
show 1 more comment
touching the file first confirms that you actually have the ability to create the file, rather than wasting time in an editor only to find out that the filesystem is read-only or some other problem.
6
Andtouchallows you to dochmod +xafterwards, before editing, which would make sense if you are writing a#!/bin/bashshell script.
– Aaron McDaid
Jun 3 '16 at 10:02
11
Alternatively, you can just do:wfirst thing after startingvithough. (and:chmod +x %if you want to make it executable)
– Stéphane Chazelas
Jun 3 '16 at 10:36
26
touchmay fail wherevi's:w!would have succeeded though (for instance iffileexists but you're not its owner and don't have write access to it, while you have write access to the current directory). Conversely,touchmay succeed butvi's:wor: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 %(someviimplementations likevimcan 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
|
show 1 more comment
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.
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.
answered Jun 2 '16 at 22:04
DopeGhotiDopeGhoti
45.8k55988
45.8k55988
6
Andtouchallows you to dochmod +xafterwards, before editing, which would make sense if you are writing a#!/bin/bashshell script.
– Aaron McDaid
Jun 3 '16 at 10:02
11
Alternatively, you can just do:wfirst thing after startingvithough. (and:chmod +x %if you want to make it executable)
– Stéphane Chazelas
Jun 3 '16 at 10:36
26
touchmay fail wherevi's:w!would have succeeded though (for instance iffileexists but you're not its owner and don't have write access to it, while you have write access to the current directory). Conversely,touchmay succeed butvi's:wor: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 %(someviimplementations likevimcan 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
|
show 1 more comment
6
Andtouchallows you to dochmod +xafterwards, before editing, which would make sense if you are writing a#!/bin/bashshell script.
– Aaron McDaid
Jun 3 '16 at 10:02
11
Alternatively, you can just do:wfirst thing after startingvithough. (and:chmod +x %if you want to make it executable)
– Stéphane Chazelas
Jun 3 '16 at 10:36
26
touchmay fail wherevi's:w!would have succeeded though (for instance iffileexists but you're not its owner and don't have write access to it, while you have write access to the current directory). Conversely,touchmay succeed butvi's:wor: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 %(someviimplementations likevimcan 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
11
11
Alternatively, you can just do
:w first thing after starting vi though. (and :chmod +x % if you want to make it executable)– Stéphane Chazelas
Jun 3 '16 at 10:36
Alternatively, you can just do
:w first thing after starting vi though. (and :chmod +x % if you want to make it executable)– Stéphane Chazelas
Jun 3 '16 at 10:36
26
26
touch may fail where vi's :w! would have succeeded though (for instance if file exists but you're not its owner and don't have write access to it, while you have write access to the current directory). Conversely, touch may succeed but vi's :w or :w! may fail if you're owner of the file, but don't have write access to it nor to the current directory (you'd be able to work around it with :!chmod +w % (some vi implementations like vim can do that automatically upon :w!)).– Stéphane Chazelas
Jun 3 '16 at 10:51
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
|
show 1 more comment
Apart from the given answers, one advantage of touch is that any other user/terminal editing the same file while you touched it , will receive a warning when they try to save any changes.
WARNING: The file has been changed since reading it!!!
Do you really want to write to it (y/n)?
This would alert them even though you have not made any changes per se and only touched the file.
good point, so it's essentially a good way to discover if people is using/editing the same file and warn them at the same time. I see a clear benefit of that in a multi-tenant environment!
– Fawix
Jun 3 '16 at 13:12
@Fawix then you should update this answer Aswell
– Kiwy
Jun 3 '16 at 18:16
add a comment |
Apart from the given answers, one advantage of touch is that any other user/terminal editing the same file while you touched it , will receive a warning when they try to save any changes.
WARNING: The file has been changed since reading it!!!
Do you really want to write to it (y/n)?
This would alert them even though you have not made any changes per se and only touched the file.
good point, so it's essentially a good way to discover if people is using/editing the same file and warn them at the same time. I see a clear benefit of that in a multi-tenant environment!
– Fawix
Jun 3 '16 at 13:12
@Fawix then you should update this answer Aswell
– Kiwy
Jun 3 '16 at 18:16
add a comment |
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 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.
answered Jun 3 '16 at 10:19
amisaxamisax
1,521515
1,521515
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
add a comment |
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
add a comment |
Apart from the accepted answer:
It is worth noting that touch is used to update file timestamps. If you use touch on a file that exists, it will then update the files timestamp to the current date and time. If the file does not exist, it creates an empty file with the current date and time as the timestamp.
vi, on the other hand, does not create a new file unless you write to it.
For example, if I typed vi test.txt, typed some notes, then typed :q!; test.txt would not exist.
What about the benefit described in the accepted answer?
– JBentley
Jun 5 '16 at 11:38
@JBentely: thanks, I just forgot to update.
– Peschke
Jun 5 '16 at 18:23
add a comment |
Apart from the accepted answer:
It is worth noting that touch is used to update file timestamps. If you use touch on a file that exists, it will then update the files timestamp to the current date and time. If the file does not exist, it creates an empty file with the current date and time as the timestamp.
vi, on the other hand, does not create a new file unless you write to it.
For example, if I typed vi test.txt, typed some notes, then typed :q!; test.txt would not exist.
What about the benefit described in the accepted answer?
– JBentley
Jun 5 '16 at 11:38
@JBentely: thanks, I just forgot to update.
– Peschke
Jun 5 '16 at 18:23
add a comment |
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.
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.
edited Jun 5 '16 at 18:22
answered Jun 2 '16 at 22:11
PeschkePeschke
2,606925
2,606925
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jun 3 '16 at 22:02
Adam KatzAdam Katz
2,2501221
2,2501221
add a comment |
add a comment |
There is no benefit to touching first; vi will create the file if it does not exist.
The accepted answer says it checks whether you can write there before wasting time in an editor. True, but now you'll be wasting time typing touch every time. Not being able to write somewhere is fairly exceptional compared to how often it will just work (as long as you remember sudo for files outside your home directory or /tmp, or are logged in as root).
Just open up the editor and do what you want, then try to save the file. If it doesn't work, even with :w!, save it elsewhere (:w ~/asdf) and fix the problem. Once it's fixed, you can copy the file contents from the temporary file to the original: cat ~/asdf > /mnt/example.txt && rm ~/asdf. The reason we use cat instead of mv or cp is to use the destination's permissions and other attributes.
Moreover, for some more advanced command line usage, you could background vi with Ctrl+Z while you fix the problem (or use :suspend, or :sus), and finally fg it again to run the write command.
Edit: post improved in response to /u/G-Man's comments. Thanks!
3
There are some security issues here: (1) If the file/mnt/example.txtis not supposed to be world-readable, but your umask is set to something permissive like 22, then/tmp/asdfwill 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.txtis 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/tmpmay prevent this.) (3) The commandmv /tmp/asdf /mnt/example.txtwill totally destroy the current/mnt/example.txtand replace it with/tmp/asdf. You want to replace the content of/mnt/example.txtwith the content of/tmp/asdf. By usingmv, you set up/mnt/example.txtto have the same attributes as/tmp/asdf— probably owned by you and world-readable. It might be better tocp /tmp/asdf /mnt/example.txtor evencat /tmp/asdf > /mnt/example.txt. … P.S. (4) You can suspendviby 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
add a comment |
There is no benefit to touching first; vi will create the file if it does not exist.
The accepted answer says it checks whether you can write there before wasting time in an editor. True, but now you'll be wasting time typing touch every time. Not being able to write somewhere is fairly exceptional compared to how often it will just work (as long as you remember sudo for files outside your home directory or /tmp, or are logged in as root).
Just open up the editor and do what you want, then try to save the file. If it doesn't work, even with :w!, save it elsewhere (:w ~/asdf) and fix the problem. Once it's fixed, you can copy the file contents from the temporary file to the original: cat ~/asdf > /mnt/example.txt && rm ~/asdf. The reason we use cat instead of mv or cp is to use the destination's permissions and other attributes.
Moreover, for some more advanced command line usage, you could background vi with Ctrl+Z while you fix the problem (or use :suspend, or :sus), and finally fg it again to run the write command.
Edit: post improved in response to /u/G-Man's comments. Thanks!
3
There are some security issues here: (1) If the file/mnt/example.txtis not supposed to be world-readable, but your umask is set to something permissive like 22, then/tmp/asdfwill 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.txtis 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/tmpmay prevent this.) (3) The commandmv /tmp/asdf /mnt/example.txtwill totally destroy the current/mnt/example.txtand replace it with/tmp/asdf. You want to replace the content of/mnt/example.txtwith the content of/tmp/asdf. By usingmv, you set up/mnt/example.txtto have the same attributes as/tmp/asdf— probably owned by you and world-readable. It might be better tocp /tmp/asdf /mnt/example.txtor evencat /tmp/asdf > /mnt/example.txt. … P.S. (4) You can suspendviby 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
add a comment |
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!
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!
edited Feb 2 at 6:16
Solomon Ucko
1076
1076
answered Jun 4 '16 at 11:21
LucLuc
9601818
9601818
3
There are some security issues here: (1) If the file/mnt/example.txtis not supposed to be world-readable, but your umask is set to something permissive like 22, then/tmp/asdfwill 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.txtis 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/tmpmay prevent this.) (3) The commandmv /tmp/asdf /mnt/example.txtwill totally destroy the current/mnt/example.txtand replace it with/tmp/asdf. You want to replace the content of/mnt/example.txtwith the content of/tmp/asdf. By usingmv, you set up/mnt/example.txtto have the same attributes as/tmp/asdf— probably owned by you and world-readable. It might be better tocp /tmp/asdf /mnt/example.txtor evencat /tmp/asdf > /mnt/example.txt. … P.S. (4) You can suspendviby 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
add a comment |
3
There are some security issues here: (1) If the file/mnt/example.txtis not supposed to be world-readable, but your umask is set to something permissive like 22, then/tmp/asdfwill 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.txtis 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/tmpmay prevent this.) (3) The commandmv /tmp/asdf /mnt/example.txtwill totally destroy the current/mnt/example.txtand replace it with/tmp/asdf. You want to replace the content of/mnt/example.txtwith the content of/tmp/asdf. By usingmv, you set up/mnt/example.txtto have the same attributes as/tmp/asdf— probably owned by you and world-readable. It might be better tocp /tmp/asdf /mnt/example.txtor evencat /tmp/asdf > /mnt/example.txt. … P.S. (4) You can suspendviby 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
add a comment |
There's no benefit. vi will create a file if doesn't exist.
add a comment |
There's no benefit. vi will create a file if doesn't exist.
add a comment |
There's no benefit. vi will create a file if doesn't exist.
There's no benefit. vi will create a file if doesn't exist.
edited Jun 2 '16 at 22:18
answered Jun 2 '16 at 22:03
ronakgronakg
1394
1394
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jun 3 '16 at 10:26
malyymalyy
1,03767
1,03767
add a comment |
add a comment |
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 "$@"
add a comment |
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 "$@"
add a comment |
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 "$@"
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 "$@"
answered Jun 5 '16 at 17:56
alexisalexis
4,06421526
4,06421526
add a comment |
add a comment |
vi is used to edit a file as user, while touch can set the timestamp on it and is mostly used in scripts and such.
Another way to create a file is:
>newfile.txt
1
This is unreliable because it's not (afaik) specified in the POSIX spec. For example, Inzsh, it's identical tocat >newfile.txtand 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
add a comment |
vi is used to edit a file as user, while touch can set the timestamp on it and is mostly used in scripts and such.
Another way to create a file is:
>newfile.txt
1
This is unreliable because it's not (afaik) specified in the POSIX spec. For example, Inzsh, it's identical tocat >newfile.txtand 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
add a comment |
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
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
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, Inzsh, it's identical tocat >newfile.txtand 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
add a comment |
1
This is unreliable because it's not (afaik) specified in the POSIX spec. For example, Inzsh, it's identical tocat >newfile.txtand 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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jun 4 '16 at 11:52
GiannakopoulosJGiannakopoulosJ
442213
442213
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Sep 3 '16 at 0:51
PremrajPremraj
1,10011117
1,10011117
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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