1753770 Members
5144 Online
108799 Solutions
New Discussion

Re: touch before vi

 
SOLVED
Go to solution
Leo The Cat
Regular Advisor

touch before vi

Hi

i've always create my files with touch before vi but i've forgot the reason of this old practice.

Regards
Den
6 REPLIES 6
Srikanth Arunachalam
Trusted Contributor

Re: touch before vi

Hi ,

There is no advantage in using touch command to create a new file. You may directly use vi to edit a new file without using "touch ".

If all you want is for the file to exist, and donâ t care if it is empty, you can use the â touchâ command.

Thanks,
Srikanth
Ralph Grothe
Honored Contributor

Re: touch before vi

I can't remember either for what reason you used a pre-touch?
Could it be that you wanted to created a file that is only read and writable by the owner,
as with certain dot files that contain sensitive data such as passwords?
Maybe something similar to this?

$ (umask 0066; vi ~/.bla.cf)
Madness, thy name is system administration
James R. Ferguson
Acclaimed Contributor
Solution

Re: touch before vi

Hi Den:

I suspect that you pre-created (with 'touch') the file such that when you exited the 'vi' session you did not have to first name the file (e.g. 'f myname' then 'wq!').

You circumvent this by starting 'vi' with the name of the file:

# vi myfile

Regards!

...JRF...
Denver Osborn
Honored Contributor

Re: touch before vi

or could be that you used touch to create the file so that another user couldn't come along and create an empty file of the same before you've had a chance to write it using vi.

If user1 runs 'vi foo' and user2 runs 'touch foo' before user1 does ':w' then user2 owns the file that user1 would attempt to be writing to. Right?

-denver
Hein van den Heuvel
Honored Contributor

Re: touch before vi

I like Denver reason for doing a touch first. It sort of puts a stake in the ground.
vi will not pre-create a file, only on (w) exit.

Doing the touch will also from protecting against bing the wrong user for a certain edits. I've been a 'normal' user all too often when trying to edit /etc/group only to find out on exit. (yeah, after extensive edits I just :w ~/tmp and take it from there. (Zelfs een ezel stoot zich, in het gemeen, niet tweemaal aan dezelfde steen. )

$ touch /bin/x
touch: /bin/x cannot create

It does not protect against no-read/write:
$ touch x
$ chmod 0 x
$ ls -l x
---------- 1 hein 513 0 Dec 28 13:12 x
$ cat x
cat: Cannot open x: Permission denied
$ touch x
$ ls -l x
---------- 1 hein 513 0 Dec 28 13:13 x
:
$vi x
:wq
---> "x" Permission denied

fwiw,
Hein.




TwoProc
Honored Contributor

Re: touch before vi

Hein had a good useful example.

I use it when I have a file I want to fill up with data, or enter text and give it to someone else, and let them move it to their home directory when I'm done.

$> touch /var/tmp/somefile.txt
$> chmod g+rw /var/tmp/somefile.txt
$> chgrp mygrp /var/tmp/somefile.txt
$> chown someuser /var/tmp/somefile.txt
$> [ edit file or run process to fill that file , even in background... ]

Then I send them an email telling them where the file is, and tell them to get it, and if it's not gone in 1 hour (some time limit really), it's going to get wiped out.

$> mailx .... (mail them a mesg about file)
$> at .... (delete the file in /var/tmp)

If I don't want to use at - I just have a sleep command in front...
$> nohup (sleep 3600; rm /var/tmp/myfile.txt) >/dev/null 2>&1 &

This keeps me from having to "sudo" or become a higher power than necessary to give them their requested data, keeps me from having to make publicly readable file (yech), and keeps me from having to "keep tabs" on the file after I've delivered it to a developer. They quickly learn to get the file when the gettin's good, or lose the file and have to re-request).

Of course, if this is secure data, then this method is not doable, but it works for lots of things.
We are the people our parents warned us about --Jimmy Buffett