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
Use &&
to conditionally run the second command if the first is successful:
$ sudo touch .htaccess && sudo chmod 766 .htaccess
&&
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.
Jun 29, 2012 at 12:14
scp [email protected]:file1 && scp [email protected]:file2 && scp [email protected]:file3
and I have set password on my SSH key, it is asking me for password all the time, is there a way to combine it???
Dec 5, 2018 at 5:48
Semicolons will let you fit multiple commands on a single line like this:
sudo touch .htaccess; sudo chmod 766 .htaccess
&&
instead of ;
when running interdependent commands so that it stops if a command fails.
Jun 29, 2012 at 12:16
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'
&&
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).
Jun 29, 2012 at 12:12