Skip to main content

How to use sudo and output redirection

It often happens that you need to echo or cat something to some config file, or maybe a log, or something else owned by root. It also often happens that you try to run it as a regular user and get a permission denied error. And normally you'd just prepend your command with sudo and be done with it, but output redirects are not that easy: current shell will handle them itself, they will not be passed to sudo and you will still have your permission problem.

Luckily, the fix is easy: instead of prepending the command, you just replace > somefile with | sudo tee somefile and >> somefile with | sudo tee -a somefile whatever the command before was. You may also add > /dev/null if you don't want if to echo everything.
Example:

echo "truncated" | sudo tee /var/log/toolong.log
echo "additional log record" | sudo tee -a /var/log/toolong.log

Of course, this is not the only way and it's not always the best but it works all the time, it's quite straight-forward (doesn't require any escaping or changing the original command in any way), and it doesn't require any additional programs to be installed.

Feel free to write about your preferred method in comments.

And a bonus tip about tee, not sudo-related:

wget -O - http://example.com/dvd.iso \
  | tee >(sha1sum > dvd.sha1) \
        >(md5sum > dvd.md5) \
  > dvd.iso

Popular posts from this blog

Using virtualenv for more than Python projects

Sorry, it's not a complete instruction, just a thought. It occurred to me (some time ago) that Python's virtualenv is, essentially, a simplified version of system "prefix", it has bin, lib, include, and can have more stuff when needed. If you're willing to experiment (you'll probably have to set a few additional environment variables and/or build flags but that's no big deal), you can install various other tools there up until you have a complete system with its own compiler and complete set of libraries although it's much simpler to keep using system compiler and libraries only complimenting them when needed. Granted, prefixes are nothing new, people were using /opt (and their home directory) this way since the beginning of time. But with little help of virtualenv-wrapper or pyenv you can easily switch between them and isolate environments better. Binaries and stuff installed in virtualenv would override system defaults but only when venv is activat

Huawei TalkBand B3 (active) review

Despite the fact that no manufacturer ever sent me any free gadget for review, I'm continuing doing it. Maybe I'll become a popular reviewer and they will change their mind. This post will be the first in this year's wearable gadget reviews. To put it into perspective for those who don't know me, I'm not a fitness person, like at all. I eat healthy, I walk kinda a lot, I do some aerobics and occasional cardio but that's it. I'm too lazy even for jogging. But, for some reason, I currently have not one, not too, but three fitness trackers on my wrists. Yeah, crazy, I know, but that was the only way to compare them properly. By the way, wearing TalkBand on the same wrist with anything else is super inconvenient, you can hardly take it out for calls. But more on that later. Why do I need any fitness tracker? Apart from knowing time, I like to know how active I'm during the day, and, more importantly, track my sleep. I have some issues in that department so

ORICO — small things that stand out

More than once I've written about stuff made by Big Brand manufacturers so I decided to balance things out by writing about a mostly-unknown but good things. In short, if you need something small like HDD enclosure or some cable or USB hub or a dongle of some kind, check if ORICO makes it and chances are you won't be disappointed. It's usually slightly more expensive than low-end stuff from aliexpress but, in my opinion, it's usually worth it. Disclaimer. This post is a shameless promotion of their brand although, unfortunately, nobody paid me for it or even sent me any free stuff. Guys from ORICO and not, if you read this, I'll gladly review whatever free goodies you send me :) I only do honest reviews so better make sure to send the good stuff. At this point I own a USB hub, three different 2.5" HDD enclosures, and a precision screwdriver set from them. And probably something else I don't remember. Here are the pics: USB hub in its natural habitat