Search Our Database

How to Remove Old Logs from Linux Server Using SSH

Last updated on |

Introduction

Old logs on a Linux server refer to archived or outdated log files that accumulate over time, taking up disk space. Removing these logs helps maintain server storage efficiency and prevents unnecessary space usage. This guide is for Linux server administrators who want to delete old log files using SSH, providing a straightforward method to free up disk space effectively.

Important Notes:

  • Review log contents if necessary before deleting any files.
  • The log will not be able to restore once it is deleted.

 

Prerequisites

  • SSH access to the server with root privileges
  • Basic knowledge of Linux commands

 

Step 1: Access to your server via SSH

  • SSH into the server and gain root privileges by command below.
    sudo su -

 

Step 2: Verify current server disk usage

  • Check the current disk usage by using this command.
    **You can determine if /var or /var/log directories are on a separate partition and their specific disk usage.

    df -h

 

Step 3: Verify the logs usage

  • Check disk usage of the log directory by using this command.
    du -sh /var/log

  • Navigate to the log directory.
    cd /var/log

  • List all the log files with their details
    ls -la

  • To check disk usage of the files.
    • For a specific file:
      du -sh <filename>

    • For all files in the current directory:
      du -sh *

 

Step 4: Identifying old logs

  • Understand the naming convention of log files. Old logs typically follow the pattern: <log_name>-<date> or <log_name>.log.<number>
    For example:

    • hawkey.log-20230507
    • btmp-20231101
    • homedir.log.1
    • homedir.log.2
DO NOT DELETE FILES WITHOUT DATE AND NUMBER SUFFIX AS THEY ARE THE CURRENT LOG FILES IN USE.

 

Step 5: Removing old logs

  • Remove the old logs with the command below. **Use the removal command with caution.
    rm <old_log_filename>

  • Confirm file deletion when prompted: Type y and press Enter to proceed or n and press Enter to cancel.
  • Upon accessing the /var/log directory, you’ll encounter various log files and subdirectories. To differentiate between files and directories, you can observe the file permission notation in the listing:
    • A directory is denoted by a d at the start of the permission string, (e.g., drwxr-xr-x).
    • A regular file is indicated by a – at the beginning of the permission string, (e.g., -rw-r–r–).
  • Use the command to view detailed file permissions along with other information like file size and last modified date.
    ls -la

 

Additional method:

  • If you need to clear the contents of a log file without actually removing the file itself, you can use the following Unix/Linux shell command:
    echo > <file_name>.log

 

Conclusion

After following these steps, you should have successfully removed old log files and freed up disk space on your server. It is good practice to regularly monitor disk usage and manage log files to prevent potential issues.

 

For additional assistance or if you encounter any issues, please contact our support team at support@ipserverone.com.

 

Article posted on 02 November 2023 by Tze Hing.