


Skill pack! 5 ways to clear or delete the contents of large files in Linux
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

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

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

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
In the following command, if represents the input file and of represents the output file.
# dd if=/dev/null of=access.log

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

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

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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

VS Code One-step/Next step shortcut key usage: One-step (backward): Windows/Linux: Ctrl ←; macOS: Cmd ←Next step (forward): Windows/Linux: Ctrl →; macOS: Cmd →

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

To install Laravel, follow these steps in sequence: Install Composer (for macOS/Linux and Windows) Install Laravel Installer Create a new project Start Service Access Application (URL: http://127.0.0.1:8000) Set up the database connection (if required)

Visual Studio Code (VSCode) is a cross-platform, open source and free code editor developed by Microsoft. It is known for its lightweight, scalability and support for a wide range of programming languages. To install VSCode, please visit the official website to download and run the installer. When using VSCode, you can create new projects, edit code, debug code, navigate projects, expand VSCode, and manage settings. VSCode is available for Windows, macOS, and Linux, supports multiple programming languages and provides various extensions through Marketplace. Its advantages include lightweight, scalability, extensive language support, rich features and version
