How to empty a file with sudo?

For a running process, the most safe way to empty its log file is use the ‘>’ command. This command will keep the file’s inode unchanged, so that the process continues writing to its log file.

If you deleted a log file, in most cases, you have to restart the process to make it have an updated file descriptor for log writing, otherwise there will be no log written any more.

Empty a log file:

$ >/home/logger/collector/NFS_logs.log

You may not have enough permission to run this command, and get error like this,

$ >/home/logger/collector/NFS_logs.log

-bash: /home/logger/collector/NFS_logs.log: Permission denied

Then how can we bypass it? It’s easy, just do it like this,

$ echo | sudo tee /home/logger/collector/NFS_logs.log

This command will empty the log file with privileges permission, right? Just think a bit more on your daily operations.

Share Button

Leave a comment

Your email address will not be published. Required fields are marked *