Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Trying to create a file auto.home under /etc and write this to the file:

I am not doing it with commandline, but bash script.

Tried this but didnt work: cat > /etc/auto.home

share|improve this question

closed as unclear what you're asking by Braiam, guntbert, Richard, mikewhatever, Sylvain Pineau Apr 6 at 21:06

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

Next time, please include the exact error messages you got (Permission denied in this case), it makes it easier to spot the problem and "it did not work" is not very useful. –  terdon Apr 5 at 17:16

2 Answers 2

up vote 2 down vote accepted

You can use "EOL" to indicate when you want to end writing to your file using cat as follows:

#!/usr/bin/env bash
cat > temp_file << EOL
sudo mv temp_file /etc/<filename>

This would keep writing to temp_file, until it gets "EOL" meaning End of Line.

Refered from: A similar question on SO

share|improve this answer
This makes no difference, using a here-doc does not change the fact that your redirection needs root privileges and you will still get a permission denied error, try it. –  terdon Apr 5 at 17:15
@terdon: Thanks a lot, had forgot about the redirection and sudo usage, edited my question adapting from your answer. –  i08in Apr 5 at 17:18
And I changed my downvote to an upvote :). Yup, this should work perfectly well. By the way, the EOL is any string of characters, it does not need to be EOL, that's just convention. You could just as easily run cat > foo << ARGH blah blahb ARGH. –  terdon Apr 5 at 17:22

You can't use sudo for redirection, the command will be run with root privileges but the > (redirection) will not so you'll get a "Permission denied" error. Using here-docs as Jobin's answer does will not help.

Instead, you can use one of these approaches:

echo "" | sudo tee -a /etc/auto.home

tee lets you append (-a) to a file, since you're running it with sudo it will work.

Alternatively, use a temp file and move it:

echo "" > home.auto && sudo mv home.auto /etc
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.