Search Our Database

How to Remove Old Logs from Linux Server Using SSH

Last updated on |

Properly managing disk space is essential for maintaining optimal server performance. Over time, log files can accumulate and consume a significant amount of disk space. This guide will walk you through the process of safely removing old log files from your server using Secure Shell (SSH).

 

1. Checking Server Disk Usage

Initiate an SSH connection to your server. You can check this KB for the steps: https://www.ipserverone.info/knowledge-base/how-to-login-to-your-linux-instance-via-ssh/

Login as root user:

sudo su -

After that, check the current disk usage by using this command:

df -h

You can determine if /var or /var/log directories are on a separate partition and their specific disk usage.

 

2. Examining the /var/log Directory

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 *

3. Identifying and Removing Old Log Files

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

For example:

  1. hawkey.log-20230507
  2. btmp-20231101
  3. homedir.log.1
  4. homedir.log.2

DO NOT DELETE FILES WITHOUT DATE AND NUMBER SUFFIX AS THEY ARE THE CURRENT LOG FILES IN USE.

 

4. To remove an old log file

Navigate to the log directory if not already there:

cd /var/log

List the files to identify old logs:

ls -la

Use the removal command with caution:

rm <old_log_filename>

Confirm file deletion when prompted: Type y and press Enter to proceed or n 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

Important Notes:

  • It is highly recommended to back up log files before deletion, as they cannot be restored once removed.
  • Review log contents if necessary before deleting any files.

DO NOT DELETE FILES WITHOUT DATE AND NUMBER SUFFIX AS THEY ARE THE CURRENT LOG FILES IN USE.

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.