Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I want to append this text:

<Directory "/var/www/*">
    Order allow,deny
    Allow from all
    AllowOverride All
</Directory>

to the file /etc/apache2/apache2.conf

I have access via SSH but I don't know how to use VIM. I would like to do this via a command.

share|improve this question
1  
Maybe I'm missing something, but why don't you use nano? –  luri Feb 9 '11 at 17:56
add comment

4 Answers 4

up vote 5 down vote accepted

Use:

nano /etc/apache2/apache2.conf

(you may need to use sudo)

This will give you a command line text editor that works much like normal text editors. Use the arrow keys to navigate. Backspace, enter, etc. work as normal.

To save, press Ctrl+O and use Ctrl+X to exit. For help, press Ctrl+G from inside nano, or use man nano.

It should look something like this:

nano

share|improve this answer
    
Very helpful thank you!! :) –  JD Isaacks Feb 9 '11 at 18:05
add comment

This syntax is called "HERE documents":

sudo tee -a /tmp/file <<EOF
<Directory "/var/www/*">
    Order allow,deny
    Allow from all
    AllowOverride All
</Directory>    
EOF

This solution is better than using ctrl-d since it can be used inside shell scripts.

share|improve this answer
    
I would agree with 'for use inside shell scripts, you will want to use HERE document syntax' . –  belacqua Feb 9 '11 at 19:29
    
btw, sometimes ctrl-d doesn't work. I once had to paste something in a file from a xen console running a initrd shell with incomplete terminal support; ctrl-c, ctrl-d etc didn't work, so I had to resort to HERO document also interactively. –  ithkuil Feb 10 '11 at 9:30
1  
That cat is completely useless, just the tee is enough. sudo tee -a /tmp/file << EOF. –  geirha Mar 3 '11 at 7:47
    
yes, cat is often abused –  ithkuil Mar 3 '11 at 16:59
add comment

Here's an easy way to do it, using cat .

% cat - >> testf
one
two
   three
four

You terminate your input with the CTRL-D .

This takes interactive input from cat (i.e., whatever you type in), and appends it to the existing file testf .

testf (with two original lines intact) will now look like this:

original line 1
original line 2

one
two
   three
four

As other answers have illustrated, you will need special syntax when editing files which you don't have write permission on. I find it easier to just switch to the root user for this, i.e., sudo su . But another easy method is to use tee with the append flag set, and called with sudo:

sudo tee -a >> config.conf

share|improve this answer
add comment

The nano editor is more friendly (sudo apt-get install nano if not available).

echo "<Directory \"/var/www/*\">"  > out_file
echo "    Order allow,deny"       >> out_file
echo "    Allow from all"         >> out_file
echo "    AllowOverride All"      >> out_file
echo "</Directory>"               >> out_file

cat out_file | sudo tee -a /etc/apache2/apache2.conf
share|improve this answer
    
You should make a backup first when blindly editing like this - it is very easy to mistype something and end up clobbering the whole file. –  dv3500ea Feb 9 '11 at 18:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.