Truncating log file linux

How to reliably clear a Linux log file

Have you ever found your Linux server running out of disk space, only to discover a single log file taking up gigabytes of storage?

Well, I have 😅. Recently, in one of the servers I’m managing, I detected a 5 GB (and steadily increasing) /var/log/maillog file.

Turned out to be an issue with an incomplete removal of a hosting control panel tool that was causing the log file to be written to every second.

After fixing the issue that was causing this non-stop growth of the log, I began looking for a way to reliably get the disk space back.

I had a sneaky suspicion that simply rm ‘ing and recreating the file would not be such a great idea.

Turns out my hunch was right. My investigation led me to learn that when a process opens a file — either for read or write, it’s actually holding a handle to that file’s inode.

inode is a Linux filesystem data structure that’s assigned to each created file. Its purpose is to store various metadata about the file.

If the file gets deleted by a different process, this may cause other programs that reference that inode to malfunction. Also, the disk space is not freed up until processes close their handle on it.

Moreover, file ownership and permissions will most likely get messed up, as the newly created file won’t have the same inode as the deleted file.

File truncation

Ok, so deleting and recreating the log file is not a viable solution. What can we do instead to free up that disk space?

We need the file’s inode to remain the same, so we’ll truncate its contents without deleting it.

Manually editing the file is not clean solution a at all. What if the file gets written to by another process while we’re editing it?

To truncate the log file we can rely on Linux’s standard output redirect operator to redirect the output of nothing to the file:

Читайте также:  Вирусы для линукс есть ли

Or, we can use command truncate , which truncates the file to a precise length in bytes:

truncate -s 0 /var/log/maillog 

Truncating a file is also much faster than deleting the file, recreating it, and setting the correct ownership and permissions.

Log rotation

Before we wrap up, it’s important to bring up the topic of log rotation.

In a more critical/production scenario, you may need to store the logs for archive. Log rotation helps us achieve this, by renaming and compressing old log files, making them ready to be backed up. logrotate is the Linux tool that achieves precisely this.

After you’ve backed up a rotated log file, it’s fine to just remove it and gain that disk space back.

References

Some resources that helped me write this article:

Built with 11ty and hosted on Netlify. Source code available on GitHub.

Источник

How to Empty a Log File in Linux

In this tutorial, you’ll learn various ways to truncate a log file and delete its content without deleting the file itself.

You’ll find yourself in situations where you need to empty a file. This often happens when you have huge log files and How would you do that?

One not so clean way would be to remove the file and then create a new file. But this is not a good idea. It won’t be the same file; the timestamp (atime, mtime etc) will be different along with other file permissions.

Instead of creating a new empty file, you can delete its content. So, how do you empty a file in Linux? How to clear a file from all of its content without deleting the actual file?

4 ways to empty a file in Linux

There are several ways you can empty a file without actually deleting the file. Let me show you some of these methods.

Method 1: Truncate a file using truncate command

The safest way to truncate a log file is using the truncate command.

In the above command, -s is used to set/adjust the size (in bytes) of the file. When you use -s 0, it means you adjusted the file size to 0 bytes.

Method 2: Empty file using :> or >

The simplest way to empty a file is to use the command below. If the file is not in use, it will work in Bash:

While the above works only in Bash Shell, you can use a similar command for other shells:

You can also use this command to clear a file:

Method 3: Using echo command to empty file in Linux

Another way to empty a file is via echo command in Linux:

You can also use the echo command in this way:

Method 4: Use /dev/null to clear a file

You can also use the famous /dev/null and combine it with the cat command to clear a log file:

In the end…

And if you don’t have enough permissions for any of the above commands, this is the sure shot but a little dirty way to achieve it:

touch newfile mv newfile filename

If your aim was to clear log files and make some free space, you should learn about cleaning journald logs.

Читайте также:  Linux ubuntu автоматическое обновление

I hope this quick tip helped you to clear a file in Linux. Do bookmark us for more Linux quick tips.

Источник

How to empty (truncate) Log files in Linux

Empty log file using truncate command The safest method to empty a log file in Linux is by using the truncate command. Truncate command is used to shrink or extend the size of each FILE to the specified size. Where -s is used to set or adjust the file size by SIZE bytes.

  1. How do I clear log files in Linux?
  2. How do you clear a log file?
  3. How do I clear my daemon log?
  4. How do I remove 30 days old files in UNIX?
  5. How do I use Logrotate in Linux?
  6. Should I delete system logs?
  7. How do I delete app logs?
  8. Is it safe to delete log files in Windows?
  9. How do I clean my syslog?
  10. Can I delete syslog log files?
  11. Can I delete syslog 1?
  12. Where are 2 days old files Linux?
  13. Where is the last 30 days file in Linux?
  14. How do I delete old files in UNIX?

How do I clear log files in Linux?

  1. Empty File Content by Redirecting to Null. .
  2. Empty File Using ‘true’ Command Redirection. .
  3. Empty File Using cat/cp/dd utilities with /dev/null. .
  4. Empty File Using echo Command. .
  5. Empty File Using truncate Command.

How do you clear a log file?

  1. Launch Event Viewer → File (in the menu) → Options (here you’ll see the disk space in your file and how much space your saved files have consumed in your profile).
  2. Hit Disk Cleanup and then Delete Files.
  3. Now Exit and hit OK.

How do I clear my daemon log?

First. You can remove log files with rm . Also you need to restart the programs that are logging eg syslog. If the file is open when its deleted it won’t actually be deleted until the program closes it or is exited.

How do I remove 30 days old files in UNIX?

  1. Delete Files older Than 30 Days. You can use the find command to search all files modified older than X days. .
  2. Delete Files with Specific Extension. Instead of deleting all files, you can also add more filters to find command. .
  3. Delete Old Directory Recursively.

How do I use Logrotate in Linux?

  1. The logrotate configuration.
  2. Setting defaults for logrotate.
  3. Using the include option to read other configuration files.
  4. Setting rotation parameters for specific files.
  5. Using the include option to override defaults.

Should I delete system logs?

Yes, log files can be safely deleted. . However, before you delete all your log files, it might be worth keeping the most recent content of the most important log files in case you need to troubleshoot any problems.

Читайте также:  Smart array utility linux

How do I delete app logs?

  1. From the System View, click the Database Properties icon.
  2. In the Enterprise View, expand the Planning application type and the application that contains the log files you want to delete.
  3. Right-click the application, and select Delete Log.

Is it safe to delete log files in Windows?

One of the options that it may give you is to delete all of the log files. . The bottom line is that the files are typically just fine as they are. You can delete them if you want, but it’s not worth your time, in my opinion. If you’re worried about losing them, back them up first.

How do I clean my syslog?

Safely clear the logs: after looking at (or backing up) the logs to identify your system’s problem, clear them by typing > /var/log/syslog (including the > ). You may need to be root user for this, in which case enter sudo su , your password, and then the above command).

Can I delete syslog log files?

You can freely delete any log files when you’re no longer interested in its content however I would inspect these for the reason of such a huge size first and resolve the root cause of its grow.

Can I delete syslog 1?

Re: Huge /var/log/syslog and /var/log/syslog. 1. You can just delete those log files. But you’re going to have to open them up and look to see just exactly what messages are filling the log, then correct those problems that are causing all the messages.

Where are 2 days old files Linux?

  1. -exec rm -f \; (or, equivalently, -exec rm -f ‘;’ ) This will run rm -f on each file; e.g., .
  2. -exec rm -f + .
  3. -delete.

Where is the last 30 days file in Linux?

You can also search the files modified before X days. Use -mtime option with the find command to search files based on modification time followed by the number of days. Number of days can be used in two formats.

How do I delete old files in UNIX?

  1. ./my_dir your directory (replace with your own)
  2. -mtime +10 older than 10 days.
  3. -type f only files.
  4. -delete no surprise. Remove it to test your find filter before executing the whole command.

How To Install ELK Stack on CentOS 7 / Fedora 31/30/29

Install

Please follow our steps below to install and configure ELK stack tools on CentOS 7 / Fedora 31/30/29 Linux.Step 1: Install Java. . Step 2: Add ELK r.

Mycroft Mark II The Open Source Answer to Amazon Echo and Google Home That Doesn't Spy on You

Google

What is the difference between Alexa and Echo and Google home?Can Google home and Amazon Alexa work together?What is Google’s answer to Amazon’s Echo.

5 Best Video Players for Linux

Player

16 Best Open Source Video Players For Linux in 2020VLC Media Player. . XBMC – Kodi Media Center. . Miro Music and Video Player. . SMPlayer. . .

Latest news, practical advice, detailed reviews and guides. We have everything about the Linux operating system

Источник

Оцените статью
Adblock
detector