ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Although Zention's solution is not wrong it could have a unwanted side effect.
If the file you want to empty is a live file (/var/log/messages to name just one) and Zention's way of emptying is used, nothing will be written in the new file afterwards, only way to solve that is to restart the program that writes to that file (syslogd in the messages example).
The > file solution does not remove the file (no need to re-create it), it just empties the file.
Your example does empty the file, but the 'cat /dev/null' part isn't needed/wanted.
If you like to have something before the > to make it more readable, use echo ""
echo is a bash internal and faster then cat /dev/null
BTW: This is the order that bash uses to look for 'commands':
- keyword
- alias
- built-in
- function
- PATH
I gave a heads-up for the following situation (below is a bit more detailed then the previous post):
syslogd has several open files it writes to, /var/log/messages being one of those.
If you need to empty /var/log/massage you should do it using: > /var/log/messages.
This leaves the pipe that syslogd has to /var/log/messages intact and syslogd can still write to this file.
If you do this: rm /var/log/message ; touch /var/log/messages
you create a completely new file. The pipe syslogd had to the original file is broken and messages cannot be written to the new file. The only way to set up the pipe again is to restart syslogd.
Syslogd is not the only program that has open files, so be careful when you empty a file. If you do it the way I posted before, you have nothing to worry about.
I agree with druuna. For example, logrotate has the copytruncate option to prevent the move/deletion of open log files:
Quote:
copytruncate
Truncate the original log file to zero size in place after creating a copy, instead of moving the old log file and optionally creating a new one. It can be used when some program cannot be told to close its logfile and thus might continue writing (appending) to the previous log file forever.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.