5 Ways to Clear or Delete Large File Contents in Linux

5 Ways to Clear or Delete Large File Contents in Linux

Sometimes, while working with files in the Linux terminal, you may want to clear the contents of a file without opening it with any Linux command line editor. How can this be achieved? In this article, we will look at clearing file contents in a few different ways with the help of some useful commands.

WARNING: Before we proceed to look at the various methods, please note that because everything is a file in Linux, you must always make sure that the files you want to empty are not important user or system files. Clearing the contents of critical system or configuration files may result in fatal application/system errors or failures.

As I just said, here’s how to clear the contents of a file from the command line.

IMPORTANT NOTE: For the purpose of this article, we used the access.log file in the following examples.

1. Clear the file contents by redirecting to empty

The simplest way to empty or clear the contents of a file is to use shell redirection null (non-existent object) as follows:

#> access.log

2. Use 'true' command redirection to clear the file

Here we will use a symbol: is a shell builtin command, which is essentially equivalent to the true command, and it can be used as a no-op (no operation).

Another way is to redirect the output of the :or true built-in command to a file, like this:

#:> access.log 
OR 
# true > access.log

3. Use cat/cp/dd utility with /dev/null to empty the file

In Linux, the null device is basically used to discard unwanted output streams of a process, or as a suitable empty file for input streams. This is usually done through a redirection mechanism.
The /dev/null device file is therefore a special file that either deregisters (removes) any input sent to it, or its output is the same as that of the null file.
Additionally, you can empty the contents of a file by redirecting the output to it (the file) as input using the cat command via /dev/null.

#cat /dev/null> access.log

Next, we will use the cp command to empty the file contents as shown.

#cp /dev/null access.log

In the following commands, if refers to the input file and of refers to the output file.

#dd if=dev/null of=access.log

4. Use echo command to clear the file

Here you can use the echo command with an empty string and redirect it to a file like this:

#echo "" > access.log 
or # echo > access.log

Note: You should keep in mind that an empty string is not the same as null. A string is already an object, since it may be empty, whereas null simply means that no object exists.

So, when you redirect the echo command above into a file, and view the file contents using the cat command, a blank line (empty string) will be printed.

To send empty output to a file, use the -n flag which tells echo to not output the trailing newline character that caused the empty line produced in the previous command.

#echo -n “”> access.log

5. Use the truncate command to clear the file

Truncate command helps to shrink or expand the size of a file to a specified size.
You can use the -s option to specify the file size. To empty the file contents, use a size of 0 (zero), as in the next command:

# truncate -s 0 access.log

That’s all, in summary, in this article, we have covered various ways to clear or empty file contents using simple command line utilities and shell redirection mechanism.

These may not be the only practical methods that may work, so you can also let us know about any other methods not mentioned in this guide via the feedback section below.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Detailed explanation of the problem that the space is not released after the Linux file is deleted
  • Linux file management command example analysis [permissions, create, delete, copy, move, search, etc.]
  • How to deal with the problem that the file is deleted but the space is not released in Linux
  • Linux unlink function and how to delete files
  • Linux implements scheduled backup of MySQL database and deletes backup files older than 30 days
  • Linux regularly backs up the MySQL database and deletes previous backup files (recommended)
  • How to delete folders, files, and decompress commands on Linux servers
  • Comparison of the efficiency of different methods of deleting files in Linux

<<:  Two implementation codes of Vue-router programmatic navigation

>>:  MySQL 5.7.18 winx64 installation and configuration method graphic tutorial

Recommend

Draw an iPhone based on CSS3

Result:Implementation Code html <div class=...

Practical example of nested routes in vue.js Router

Table of contents Preface Setting up with Vue CLI...

Writing a rock-paper-scissors game in JavaScript

This article shares the specific code for writing...

Docker images export and import operations

What if the basic images have been configured bef...

Detailed explanation of client configuration for vue3+electron12+dll development

Table of contents Modify the repository source st...

Example of CSS3 to achieve div sliding in and out from bottom to top

1. First, you need to use the target selector of ...

How to modify the time in centos virtual machine

The one above shows the system time, and the one ...

PHP-HTMLhtml important knowledge points notes (must read)

1. Use frameset, frame and iframe to realize mult...

Summary of various implementation methods of mysql database backup

This article describes various ways to implement ...

Vue uses Echarts to implement a three-dimensional bar chart

This article shares the specific code of Vue usin...

Detailed explanation of CSS line-height and height

Recently, when I was working on CSS interfaces, I...

Example of how to deploy a Django project using Docker

It is also very simple to deploy Django projects ...