Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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 an assignment where I am required to back up the /var/log/dmesg, /var/log/syslog and /var/log/message to text files in a different folder such as the Desktop. It will also create a file called Execution.txt to record the date and time whenever the bash script is run. I do have this code that I've written yet every time I run it, it gives me:

./ line 7: /var/log/dmesg: Permission denied
./ line 8: /var/log/syslog: Permission denied
./ line 9: /var/log/message: Permission denied

However, it does create these files on the desktop, but the message and dmesg files are empty. I was wondering if this is normal or have I done something wrong? Below is the code that I have written so far. Any help would be appreciated. Thanks!


cat /var/log/dmesg l nl >> /home/administrator/Desktop/dmesg
cat /var/log/syslog l nl >> /home/administrator/Desktop/syslog
cat /var/log/message l nl >> /home/administrator/Desktop/message

echo ' ' > /var/log/dmesg
echo ' ' > /var/log/syslog
echo ' ' > /var/log/message

date >> /home/administrator/Desktop/execution.txt
share|improve this question
What does l and nl in your code refers to? – Avinash Raj Mar 25 '14 at 13:28
up vote 8 down vote accepted

I suspect you just don't have access to them from the user that is running this script. Look at the file ownerships:

$ ls -l /var/log/{dmesg,syslog,message}
ls: cannot access /var/log/message: No such file or directory
-rw-r----- 1 root   adm 86384 Mar  9 11:12 /var/log/dmesg
-rw-r----- 1 syslog adm 18553 Mar 25 13:25 /var/log/syslog

You could read from these files if you had a user in the adm group but you'd not be able to write. The first user on a system is typically a member of the adm group but if your ~/Desktop copies are empty, I'd suggest your user isn't (check with the groups command). You do have a few options.

  • You could either look at adding ACL permissions for your user. These are separate from the standard permissions and adding explicit read/write access to your user doesn't really affect anything else. Which is nice.

    sudo apt-get install acl
    sudo setfacl -m u:$USER:rw /var/log/{dmesg,syslog,message}

    This might need a reboot to take. You might even need to change your fstab. I haven't but ACLs seem to work okay here so I'm assuming that's not required any more.

  • You could change the unix permissions on the file. The safest way of doing this would be to add your user to the adm group and then to enable group write on those files:

    sudo usermod -a -G adm $USER
    sudo chmod g+w /var/log/{dmesg,syslog,message}

    You'll need to log out and in again to see the effect.

  • Or you could let anybody read and write these files. This could be a security risk so I'm not giving you code... But honestly, while possible, you probably don't want this.

  • The other option is simply running the script as root with sudo ./scriptname. The downside of this is that anything this script does will be done as root. That includes creating new files (which >> can if the file doesn't exist already) and that can be a pain for the user.

And if this isn't academic curiosity and you're actually trying to get some work done in the best possible way, there are tools that do these things for you but better. You really want to look at logrotate. This Digital Ocean tutorial is the cleanest one I can find quickly.

share|improve this answer
Thanks for the help! Added myself to the adm group and was able to run it without any problems :) – Zenkarus Mar 25 '14 at 16:57

You most probably don't have read permissions for /var/log/{dmesg,syslog,message}. So even though you have the files created on your destkop, they will be empty, since nothing was read but an attempt to write was made to a file(the one created on your desktop).

So you have two options:

  1. Get read permissions for /var/log/{dmesg,syslog,message} as follows:

    sudo usermod -a -G adm $USER #adm is the group name which owns /var/log/syslog
  2. Use sudo for reading (easier for one-time read):

    sudo cat /var/log/dmesg >> /home/administrator/Desktop/dmesg
share|improve this answer

Your problem is the following lines of your script :

echo ' ' > /var/log/dmesg
echo ' ' > /var/log/syslog
echo ' ' > /var/log/message

Unsurprisingly, since those are precisely lines 7,8 and 9 which are the ones that bash is complaining about.

These commands are attempting to delete the contents of those 3 log files in /var/log but since they are owned by root and you have no write access to them, you are getting an error. Good! I am assuming you do not want to clear out your system logs like that.

Remember that > overwrites the contents of a file, you need >> to append to it. In any case, you should never modify those files, they are used by the system and there is absolutely no reason why you would want to overwrite them. Even if you do, there are far simpler ways do do so, like rm file or even simply > file.

A cleaner way to do all this would be to use logrotate instead.

share|improve this answer
I need to be able to clean up each of the log files with the script as well – Zenkarus Mar 25 '14 at 16:58
@Zenkarus really? They want you to empty files from /var/log? Weird. Well, use rm or >/var/log/filename then but run with sudo. A cleaner way for all this would be logrotate. – terdon Mar 25 '14 at 17:01
Yes, that is one of the requirements of my assignment. Thank you for the help. – Zenkarus Mar 27 '14 at 1:45

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.