Home  >  Article  >  System Tutorial  >  Skill pack! 5 ways to clear or delete the contents of large files in Linux

Skill pack! 5 ways to clear or delete the contents of large files in Linux

王林
王林forward
2024-02-11 15:30:34887browse

When processing files under the Linux terminal, sometimes we want to directly clear the contents of the files without using any Linux command line editor to open these files. So how can we achieve this goal? In this article, we will introduce several methods to clear the contents of files with the help of some practical commands.

Note: Since everything in Linux is a file, you need to always pay attention to ensure that the files you want to clear are not important user files or system files. Clearing important system files or configuration files may cause serious application failures or system errors.

Tip:In the following example, we will use a file named access.log as an example sample.

1. Clear the file contents by redirecting to Null

The simplest way to clear or make a file blank is to redirect null (non-existent things) to the file through the shell as follows:

# > access.log
技能包!Linux 下清空或删除大文件内容的 5 种方法

2. Use ‘true’ command redirection to clear files

Below we will use the : symbol, which is a built-in command of the shell and is equivalent to the true command. It can be used as a no-op (that is, no operation is performed). Another way to clear a file is to redirect the output of the : or true built-in command to a file, as follows:

# : > access.log

# true > access.log
技能包!Linux 下清空或删除大文件内容的 5 种方法

3. Use cat/cp/dd utility and /dev/null device to clear files

In Linux, the null device is basically used to discard an output stream that is no longer needed by a process, or a blank file as an input stream. These can usually be achieved using the redirection mechanism, so /dev/null A device file is a special file that clears all input sent to it, and its output can be treated as an empty file. In addition, you can clear the file by using the cat command to display the contents of /dev/null and then redirect the output to a file.

# cat /dev/null > access.log
技能包!Linux 下清空或删除大文件内容的 5 种方法

Below, we will use the cp command to copy the contents of /dev/null to a file to clear the file, as shown below:

# cp /dev/null access.log

技能包!Linux 下清空或删除大文件内容的 5 种方法
In the following command, if represents the input file and of represents the output file.

# dd if=/dev/null of=access.log
技能包!Linux 下清空或删除大文件内容的 5 种方法

4. Use the echo command to clear the file

Here, you can use the echo command to redirect the content of the empty string to the file, as follows:

# echo "" > access.log
或者
# echo > access.log

技能包!Linux 下清空或删除大文件内容的 5 种方法
Note: You should remember that an empty string is not the same as null. A string indicates that it is a specific thing, but its content may be empty, but null means that something does not exist. For this reason, when you use the cat command to view the contents of the file after redirecting the output of the echo command as input to a file, you will see a blank line (i.e., an empty string).
To write null as output to a file, you should use the -n option, which tells echo not to output the trailing newline like the command above does.

# echo -n "" > access.log
技能包!Linux 下清空或删除大文件内容的 5 种方法

5. Use the truncate command to clear the file contents

truncate can be used to shrink or expand a file to a given size.
You can use this with the -s parameter to specify the file size. To clear the contents of the file, set the file size to 0 in the following command:

# truncate -s 0 access.log
技能包!Linux 下清空或删除大文件内容的 5 种方法

That’s all I want to introduce. In this article, we have covered several ways to clear or empty file contents by using some simple command line tools and shell redirection mechanisms.

The above is the detailed content of Skill pack! 5 ways to clear or delete the contents of large files in Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete