Skip to main content

Managing files on a server often includes tasks like clearing logs or resetting configuration files. If you need to empty a file without deleting it, there are several quick and efficient methods to achieve this.

Recently, during a review of log files, I noticed an unusually large file: access_ssl_log.processed, which was a substantial 8.77 GB. After reviewing and backing up the data, I needed to empty the file to free up space and maintain server performance.

-rw-r--r-- 2 root   root  667816325 May 30 10:19 access_ssl_log
-rw-r--r-- 2 root   root  256862604 May 30 10:19 error_log
-rw-r--r-- 2 root   root    2489417 May 30 10:19 proxy_access_ssl_log
-rw-r--r-- 2 root   root      96960 May 30 10:07 proxy_access_log
-rw-r--r-- 2 root   root    2186669 May 30 10:01 proxy_error_log
-rw-r--r-- 2 root   root 9417940348 May 29 08:49 access_ssl_log.processed
-rw-r--r-- 2 root   root  134776758 May 29 08:48 access_log.processed
-rw-r--r-- 1 root   root  598838459 May 29 08:48 access_ssl_log.webstat
-rw-r--r-- 1 root   root      17023 May 29 08:48 access_log.webstat
-rw-r--r-- 2 root   root          0 May  6 06:46 access_log
-rw-r--r-- 2 root   root    3546875 Apr  5  2022 xferlog_regular.processed
-rw-r--r-- 2 root   root          0 Apr  5  2022 xferlog_regular
-rw-r--r-- 2 root   root          0 Oct 21  2015 webmail_access_log

Here are some approaches to consider for emptying a file without deleting it:

 

Using the truncate Command

The truncate command is a straightforward tool for resizing files. You can use it to set the file size to zero, effectively emptying it.

truncate -s 0 /path/to/your/file

Explanation:

  • -s 0: Sets the size of the file to 0 bytes
  • /path/to/your/file: Replace this with the path to your file

 

Using Shell Redirection (>)

Shell redirection is a simple and widely used method to clear a file. The > operator redirects output to a file, overwriting its contents.

> /path/to/your/file

Explanation:

  • >: Redirects an empty input to the file
  • /path/to/your/file: Replace this with the path to your file

 

Using the : > Redirection

The : command is a built-in shell command that does nothing and returns a true value. When combined with the > operator, it can empty a file.

: > /path/to/your/file

Explanation:

  • :: A shell built-in command that returns true
  • >: Redirects the output of : (which is nothing) to the file
  • /path/to/your/file: Replace this with the path to your file

 

Using the echo Command

The echo command outputs the given string to a file. By providing an empty string, you can clear the file's contents.

echo -n "" > /path/to/your/file

Explanation:

  • echo -n "": Outputs an empty string
  • >: Redirects this output to the file
  • /path/to/your/file: Replace this with the path to your file

 

Using cat /dev/null

The cat command reads the contents of a file and outputs them. By redirecting the output of /dev/null (a special file that is always empty) to your target file, you can empty the file.

cat /dev/null > /path/to/your/file

Explanation:

  • cat /dev/null: Reads from /dev/null, which is always empty
  • >: Redirects this empty output to the file
  • /path/to/your/file: Replace this with the path to your file

 

The wrap

Each of these methods can efficiently empty a file on your server. Choose the one that best fits your workflow or script. Whether you prefer using built-in shell commands or specific tools like truncate, clearing a file's contents is a simple task with these techniques. For my situation, I chose to use the truncate method.