(GO) Linux sudo redirection to write information in files that only the system administrator has permission to manipulate

Source: Internet
Author: User
Tags echo command

As we all know, using echo and cooperating with command redirection is a shortcut that implements writing information to a file. This article describes how to use the echo command with the sudo command. implementations write information to files that only the system administrator has permission to do. For example, to write to the Test.asc file random content, you can: $ echo "info" > test.asc# or echo "Information" >> TEST.ASC below, if the TEST.ASC permission is set to only the root user has permission to enter Line write operation: $ sudo chown root.root test.asc then we use sudo to write the information to the TEST.ASC file after modifying the permission again with the echo command: $ sudo echo "Another line of information" >> TE St.asc-bash:test.asc:permission denied at this point, you can see that bash refuses to do so, saying it's not enough authority. This is because the redirect symbol ">" and ">>" are also commands for bash. We use sudo just to have the echo command root, but without the ">" and ">>" commands having root privileges, bash will assume that neither command has permission to write information to the TEST.ASC file. There are two ways to solve this problem. The first is to use the "sh-c" command, which 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 ' another line of information ' >> TEST.ASC" this way sudo executes a shell that executes the previous command in the root-privileged shell without any more permissions issues. 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: $ echo "Third message" | sudo tee-a test.asc Note that the "-a" option of the tee command is equivalent to the ">>" command, and if this option is removed, the tee command is equivalent to the ">" command.

(GO) Linux sudo redirection to write information in files that only the system administrator has permission to manipulate

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.