How to delete log files in Linux?

Published on Aug. 22, 2023, 12:17 p.m.

To delete log files in Linux, you can use the rm command. For example, to delete a log file called my_log.txt, you can use the following command:

rm my_log.txt

If you want to delete all log files in a directory, you can use a wildcard * to match all files with a particular extension. For example, to delete all log files with a .log extension in the current directory, you can use the following command:

rm *.log

Alternatively, you can use the find command to locate and delete log files based on certain criteria, such as the file modification time or the file name. For example, to delete all log files older than 30 days in the /var/log directory, you can use the following command:

find /var/log -type f -mtime +30 -name '*.log' -delete

This will find all files ( -type f ) with a modification time older than 30 days ( -mtime +30) and a name that matches the pattern *.log, then delete them ( -delete). Note that the find command can be a powerful tool, so exercise caution when using it to avoid accidentally deleting important files.

Tags:

log