> ## Content Index
> Fetch the complete content index at: https://linuxhandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# How to Empty a Log File in Linux
- URL: https://linuxhandbook.com/empty-file-linux/
- Published: 2019-05-07T10:46:23.000Z
- Updated: 2023-01-23T13:17:43.000Z
- Description: In this tutorial, you'll learn various ways to truncate a log file and delete its content without deleting the file itself.
- Author: Abhishek Prakash
- Tags: Tips

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](https://linuxhandbook.com/remove-files-directories/) and then [create a new file](https://linuxhandbook.com/create-file-linux/). But this is not a good idea. It won’t be the same file; the [timestamp (atime, mtime etc)](https://linuxhandbook.com/file-timestamps/) will be different along with other [file permissions](https://linuxhandbook.com/linux-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.

```
truncate -s 0 filename
```

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.

If you [view the file content](https://linuxhandbook.com/view-file-linux/), it will be empty.

### 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:

```
> filename
```

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

```
:> filename
```

You can also use this command to clear a file:

```
true > filename
```

### Method 3: Using echo command to empty file in Linux

Another way to empty a file is via [echo command in Linux](https://linuxhandbook.com/echo-command/):

```
echo > filename
```

You can also use the echo command in this way:

```
echo "" > filename
```

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

You can also [use the famous /dev/null](https://linuxhandbook.com/dev-null/) and combine it with the [cat command](https://linuxhandbook.com/cat-command/) to clear a log file:

```
cat /dev/null > file.log
```

## 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
```

You can also empty a file by [deleting all lines in Vim editor](https://linuxhandbook.com/delete-all-lines-vim/).

If your aim was to clear log files and make some free space, you should learn about [cleaning journald logs](https://linuxhandbook.com/clear-systemd-journal-logs/).

[How to Clear Systemd Journal Logs in LinuxThis quick tutorial shows you two ways to clear systemd journal logs from your Linux system.![](https://linuxhandbook.com/assets/icon-192x192.png?v=5090187721)Linux HandbookAbhishek Prakash![](https://linuxhandbook.com/content/images/2020/06/clear_logs_linux-1.jpg)](https://linuxhandbook.com/clear-systemd-journal-logs/)

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