Forward: http://blog.csdn.net/hejinjing_tom_com/article/details/7767127
avoid ' sudo echo x > ' when ' Permission denied '
A : Example
sudo echo a > 1.txt
-bash:1.txt:permission denied
B: Analysis:
Bash refuses to do so, saying it has insufficient authority.
This is because the redirect symbol ">" is also a bash command. sudo just lets the echo command have root privileges,
However, the ">" command has no root privileges, so bash will assume that the command does not have permission to write information.
C: Solution. Three kinds:
1. With the "sh-c" command, it allows bash to execute a string as a complete command, so that the scope of sudo can be extended to the entire command.
The specific usage is as follows:
sudo sh-c "echo a > 1.txt"
The same is true with Bash-c, which is now popular with bash shells.
2. With the pipe and tee commands, the command can read the information from the standard input and write it into standard output or a file.
The specific usage is as follows:
echo a |sudo tee 1.txt
echo a |sudo tee-a 1.txt//A is added meaning, equivalent to >>
The tee command works well, it receives information from the pipeline, and outputs it to the screen while writing to the file.
Linux always has some gadgets for us to think about very aptly!
3. Elevate Shell permissions.
sudo-s//refers to root permissions. Prompt is #
when you think it's time to return to normal permissions,
sudo su username//return to username permissions, prompt for $
CentOS elevation privilege: Su-
Ubuntu Elevation of Privilege: sudu-s, sudo su
Forward another post: http://digdeeply.org/archives/05232211.html
Example
12 |
sudo "echo ‘[yaf]‘ > /usr/local/php/etc/include/yaf.ini"
#Permission denied 权限不够
|
When using sudo echo ' xxx ' >/path/file, in fact sudo is used only on the echo, and redirection does not use sudo permissions, so there will be "Permission denied" situation, the solution is also very simple, is a parameter just. Add a "sh-c" to assign permissions to the entire shell.
Example
1 |
sudo sh -c
"echo ‘[yaf]‘ > /usr/local/php/etc/include/yaf.ini"
|
Another approach is to use the pipe and tee commands, which can read information from standard input and write it into standard output or a file, using the following:
Example
1 |
echo “xxxx” |
sudo tee -a
test
.txt
|
The tee command de "-a" option is equivalent to the ">>" command, if this option is removed, then the tee command is equivalent to the ">" command.
sudo echo command error: Permission denied