Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have just set up the .com of a domain that previous ran on on the same server, I have copied all the files, ran through them using bash to change the domain anywhere it was referenced, same for DB, and all went great, same with the redirect. But now I want to move the cron jobs across using bash.

I was thinking

sudo cat /var/spool/cron/sbadmin | while read -r line; do echo $line;done > /var/spool/cron/sbcomadmin

But permission is denied on the new file. How can permission be denied when I am in sudo mode?

The domain will need changing in the new cron file aswell but I was thinking I could use sed on that, either during the copy or after.

share|improve this question
up vote 1 down vote accepted

First, the loop you are using is useless. You command can be written as:

$ sudo cat /var/spool/cron/sbadmin > /var/spool/cron/sbcomadmin

This will not work. You need to use the following command instead to create the file as root:

$ cat /var/spool/cron/sbadmin | sudo tee /var/spool/cron/sbcomadmin

The tee will write the output to both stdout and the provided file. If you don't want to write to stdout, you can append > /dev/null at the end of the previous command. Also, you can use tee -a to work in append mode if needed to replace >>.

share|improve this answer
Great thanks. I just added to my question, I need to change the domains in the cron file from to, I was thinking maybe use sed, is there anyway I can incorporate this into the same command. – Liam Bailey Dec 23 '12 at 15:25
@LiamBailey: Yes, you can use sed in the command. So it comes cat file | sed 's/' | sudo tee file. – Khaled Dec 23 '12 at 15:59
so, just to confirm if I didn't want the output the command would be `cat /var/spool/cron/sbadmin > /dev/null | sudo tee /var/spool/cron/sbcomadmin or would the > /dev/null go after the filename on sudo tee? – Liam Bailey Dec 23 '12 at 21:11
@LiamBailey: The 2nd one. – Khaled Dec 24 '12 at 7:52

Assumming the userids and group information are the same.....have you thought of using cpio or tar...make the archive....send it to the other system.....restore it....make the minor edits....done. A shell loop like this is not a very efficient method especially if there are a large number of files.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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