The <, > and >> are used for input / output redirection for commands - which is a feature provided by the shell (e.g., bash). So if you type a command like sudo cat > /var/www/info.php
then the shell that receives this as input tries to open the file /var/www/info.php
and provides that file as the standard output to the sudo
command. The sudo
command is not even aware whether its output is going to a console or redirected to a file, because this is taken care of by the shell that invokes it.
If the shell you typed your command into is your login shell or another shell running in a terminal with your user id, then it has same privileges as your user id - not those of root.
So in your case, whereas the cat command is executed as root, the copying of its output to /var/www/info.php
is attempted by the shell running as a normal user, which, as expected, fails.
A workaround for such situations is to use the tee
command :
sudo tee /var/www/info.php
That will have the intended effect of putting all the text entered at the console upto ^D into the file specified as parameter.
One perhaps undersirable side-effect is that tee
will also echo the output to the stdout, so after you type each line and press enter tee
will output a copy of it back. To avoid this you can use the following variant.
sudo tee /var/www/info.php > /dev/null
Details about tee
can be had via info tee
at a terminal.
Just to clarify:
- Linux is the kernel that Ubuntu and many other distributions use.
- Linux is not an operating system.
- A Linux distribution is a set of software that includes an operating system as well as other utilities that make the operating system useful. (ie gnome)
- Because they are different things, a distribution and a kernel are not comparable items.
Ubuntu is a Linux distribution that uses the Linux kernel, the standard GNU toolkit and a whole lot of other free software stacked on top.
The command-line utilities are not provided by Linux. Most core utilities are provided by the GNU project. Some are specific to the distribution (eg apt-get
is specific to Debian-based distros). The rest come from any number of other independent sources.
Depending on your definition of 'command-line structure', every GNU distribution uses the same 'command-link structure'. I would consider android, for instance, a Linux distribution but not an GNU distribution. Most Linux distributions adhere to the Linux Standard Base. This sets out the rules for where files go, and how things are structured (amongst other things).
On the other hand, you could also say that 'command-line structure' changes based on the shell that you use.
Best Answer
&&
is a logical 'and'. The first argument is evaluated, and the second one is evaluated only if the first one returns true. The reason is that "false AND something" is always false, so no need to evaluate the second argument in this case.So using
&&
you make sure that the second command is not executed if the first one reports an error (true
is represented by the exit code0
, which indicates that there was no error). In contrast,;
executes both commands, no matter what the outcome of the first one is.Conclusion: Chaining commands with
&&
is a good habit. In contrast to;
it won't execute subsequent commands if something went wrong.