Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I would like to write the following on one line in the terminal or if there is a shorter way to create a file and set the permissions then please do comment.

sudo touch .htaccess
sudo chmod 766 .htaccess

Much appreciated

share|improve this question
add comment

3 Answers

Use '&&' to conditionally run the second command if the first is successful.

sudo touch .htaccess && sudo chmod 766 .htaccess

share|improve this answer
    
I prefer this solution for two reasons: 1. && is generally better than ; so that it stops if the first command fails, 2. it's generally better to only run the specific program needed with sudo, rather than trying to combine them into a single sudo invocation and running more code with elevated privileges than necessary. –  Chris Page Jun 29 '12 at 12:14
add comment

No, it is not.

The only thing you can make, you can combine these two commands within one sudo:

sudo sh -c 'touch .htaccess; chmod 766 .htaccess'

Another solution (witho ony one external call, but also with two commands):

sudo sh -c 'umask 011; touch .htaccess'
share|improve this answer
    
Good suggestions, but I would revise these to use && instead of ;, though. Also, in general I would just use two sudo commands, because it's overkill and riskier to run an entire shell with elevated privileges, rather than just the exact programs that need to run with them (in this case, touch and chmod). –  Chris Page Jun 29 '12 at 12:12
    
Yes, you are right, it would be better –  Igor Chubin Jun 29 '12 at 12:17
add comment

Semicolons will let you fit multiple commands on a single line like this:

sudo touch .htaccess; sudo chmod 766 .htaccess

share|improve this answer
    
That's a good suggestion, though it's a good idea to get into the habit of using && instead of ; when running interdependent commands so that it stops if a command fails. –  Chris Page Jun 29 '12 at 12:16
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.