Why can't I save a VI file after entering data?

Hi

I am new to linux, when I typed "vi FILE1" I was able to open VI editor. I added some data and I want to save the file and I tried :w but it threw me an error.

"file1" E212: Can't open file for writing
Press ENTER or type command to continue

why I am not able to save it?

I read it some where that I need permissions for that. While installing ubantu... I created and User and password with which I login.

Wont that user have admin privilages? what do I have to do to go further?

Thanks in advance

No, tjhat user does not have admin privilege.

You can use "sudo vi FILE1"

Hi thanks Tony.

It worked. but how can I give admin privilages to a new user created? or already existing user in linux.

PS: I am using UBUNTU

You an either

  1. Change the permissions of the file concerned so that other users can edit the file, e.g. create a new group (sudo groupadd newgroup), put the users who are to be allowed to edit the file into that group (sudo adduser user newgroup), make the file group owned by the new group (sudo chgrp newgroup FILE1) and make the file writeable by the group (sudo chmod g+w FILE1)

2, Even if you do not want to give your users access to sudo to do anything they like (very wise!) then you can allow users to edit this one file in particular as root by using sudo with or without providing these users having to provide a password, e.g. run visudo:

$ sudo visudo

and enter the line:

some_user ALL = NOPASSWD: /usr/bin/vi /path/to/FILE1

Or to allow all users in a group (newgroup in this example) to edit the file then add:

%newgroup ALL = NOPASSWD: /usr/bin/vi /path/to/FILE1

Replace NOPASSWD with PASSWD if you want them to still provide their own password before being able to edit the file.

Note the full path to vi so the user cannot write a script called vi and run it as root and the file to be edited is also fully pathed.

The user (or users in newgroup) can then run:

sudo /usr/bin/vi /path/to/FILE1

to edit FILE1 as if they are the root user with or without having to provide a password depending on what you put into the sudoers file.

HTH

Hi TonyFullerMalv,

Thank you very much for the detailed explanation with the commands.

---------- Post updated at 11:30 PM ---------- Previous update was at 11:27 PM ----------

Please help me with one more question. I am trying to copy files from one folder to another. but it is not working

cp/file1/homefolder/destinationfolder

or

cp/file1/homefolder/destinationfolder/file2

I know these are wrong. But please correct where these are wrong.

Thanks in advance

You need a space after 'cp' and between the source and destination.
The destination can be either a file name or a directory[folder]
The source name can be absolute or relative to the current directory.
You need to have read permissions for the source file, and write permissions for the destination directory.

cp /homefolder/file1 /destinationfolder/file2

Hi thanks for the code.

But to gain the permissions shall I use sudo all the time?

How do I use the vi text editor?

The vi text editor has three modes: command mode, input mode, and ex mode.
Command mode

When starting, vi begins in command mode. If you are ever unsure which mode you're in, press Esc to return to command mode. In command mode, you can move around with the arrow keys, or by using the vi movement keys, as follows:
h left j down k upl right
Several vi commands are listed in the table below:
CommandAction Ctrl-bGo back one page Ctrl-fGo forward one page xDelete the character the cursor is on Shift-xDelete the character before the cursor ddDelete the current line Shift-dDelete everything from the cursor to the end of the line uUndelete a line you just deleted Shift-uUndo all changes to the current line Shift-z-zSave your file and exit the vi editor
Note: In command mode, you can type a number before pressing a command key to repeat the command multiple times. For example, to delete eight lines from the cursor position, you could press 8 and then type dd .
Input mode

The input mode lets you insert or append text. To insert text before the cursor's current position, in command mode, press i . Similarly, to append after the cursor, you can type a . Remember that you can't move around with the cursor keys in this mode. When you're done entering text, press Esc to go back to command mode.
Ex mode

The ex mode is an extension of command mode. To get into it, press Esc and then : (the colon). The cursor will go to the bottom of the screen at a colon prompt. Write your file by entering :w and quit by entering :q . You can combine these to save and exit by entering :wq . However, if you're finished with your file, it's generally more convenient to type Shift-z-z from command mode.
For a more thorough list of vi command keys, see A quick reference list of vi editor commands.
:b::D:cool::wink:

Hope this works.

Thanks

Hannan Saleemi.

hi here is what I am trying.

chinnanji@ubuntu:~$ mkdir newfolder
chinnanji@ubuntu:~$ cd newfolder
chinnanji@ubuntu:~/newfolder$ mkdir folder2
chinnanji@ubuntu:~/newfolder$ cd folder2
chinnanji@ubuntu:~/newfolder/folder2$ touch file1 file2
chinnanji@ubuntu:~/newfolder/folder2$ ls
file1  file2
chinnanji@ubuntu:~/newfolder/folder2$ cd ..
chinnanji@ubuntu:~/newfolder$ cd ..

chinnanji@ubuntu:~$ cp /folder2/file2 /newfolder/file1
cp: cannot stat `/folder2/file2': No such file or directory

chinnanji@ubuntu:~$ cp /folder2/file2 /newfolder/
cp: cannot stat `/folder2/file2': No such file or directory

but when I tried to do it with out any slashes... it created a newfolder again.

please correct me

---------- Post updated at 11:35 AM ---------- Previous update was at 11:26 AM ----------

Hi Hannan,

Thanks for the handy list of commands. Till now I used to think that we have only two modes... command mode and text editor mode. the extension mode is interesting. thanks again

By putting the / at the beginning of the copy command, you are saying that the file is in a directory at the very top of the file system hierarchy. If you leave the leading slash off the cp command then the file(s) will be located relative to the current directory.
You should also change the permissions on directories that you have created so that you do not need to use the root account to write files.

Hi thanks for the explaination. sorry to ask again.

how to change the permissions on directories that are being created.

use 'chmod' to change the permissions.

At this stage, use

chmod 0666 datafile

for documents and text files

and

chmod 0777 script

for scripts and programs that you need to execute, and for new directories.

These allow everyone all permissions, but I am assuming that you are the only user.

You should only do things as the root user when you have to, so no do not use sudo all the time.

@jgt
yes I am the only user in my system. your suggestions worked out it is like a magic to me being novice

@tony
thanks and plz tell me what will be the root default password cos' I set password only to my user account created while installation.

cant my user account be the admin account in UBUNTU (Linux)? As in Windows can I give all admin privilages to my existing user account?

plz dont get annoyed if I am being dumb.

---------- Post updated 02-10-10 at 12:30 PM ---------- Previous update was 02-09-10 at 07:11 PM ----------

I read that I can give default settings to have line numbers by default if we edit .exrc file

I opened a file with vi.

this is what I have done. cos' I dont know where the particular file is present.

vi *.exrc

this opened vi editor

I added these lines

% cd
% echo 'set number' >> .exrc
%

:wq!

I opened another file now but it is not showing the line numbers.

where I was wrong?

In Ubuntu one does not normally need the root password because you use sudo instead, so my mentioning root password applies to other industry Unix es.
In Ubuntu your account is an administrator account by virtue of being in the admin group. My account in Ubuntu is in the following groups:
tony@laptop:~$ groups
tony adm dialout cdrom plugdev lpadmin admin sambashare newgroup
tony@laptop:~$

On the ~/.exrc file question you either edit it using vi to add the "set number" line OR you use the echo method to add it to the end of the file, either way if there was no ~/.exrc file presnet before then it should only contain the line:

set number

but to use some commands... i am getting denied with permissions. why is it so if the user account holds the admin privilages.

---------- Post updated at 12:37 AM ---------- Previous update was at 12:34 AM ----------

:set number works fine. but it gives line numbers only for current session. to have the line numbers every session, I need to edit .exrc

what is .exrc is it a file that can be opened with vi and can be edited? if so where it is located?

cos' i tried to open it with

vi *.exrc and added the lines... but it is not saved.

.exrc is located in your home directory. This is the directory where you are placed when you first login.
The ~ (tilde) character is a short form for "my home directory", so

vi ~/.exrc 

will edit the .exrc in your home directory whether you are in it or not.