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!

Linuxusesdecentralized,distribution-specificpackagemanagersforpatchmanagement,whileWindowsemploysacentralizedWindowsUpdatesystem.Linux'sapproachoffersflexibilitybutcanbecomplexacrossdistributions,whereasWindowsprovidesastreamlinedbutlessflexibleupdat

Virtual Data Rooms (VDRs) offer secure document storage and sharing, ideal for sensitive business information. This article explores three open-source VDR solutions for on-premises deployment on Linux, eliminating the need for cloud-based services a

Upscayl: Your Free and Open-Source Solution for High-Resolution Images on Linux Linux users who frequently work with images know the frustration of low-resolution pictures. Luckily, Upscayl offers a powerful, free, and open-source solution. This des

The terminal emulator landscape is evolving rapidly, with developers leveraging modern hardware, GPU acceleration, containerization, and even AI/LLMs to enhance console experiences. Enter Ghostty, a new open-source, cross-platform terminal emulator

Innotop: Powerful MySQL monitoring command line tool Innotop is an excellent command line program, similar to the top command, used to monitor local and remote MySQL servers running under the InnoDB engine. It provides a comprehensive set of features and options to help database administrators (DBAs) track various aspects of MySQL performance, troubleshoot issues and optimize server configuration. Innotop allows you to monitor critical MySQL metrics, such as: MySQL replication status User statistics Query list InnoDB buffer pool InnoDB I/O Statistics Open table Locked table etc… The tool regularly refreshes its data to provide server status

Restic: Your Comprehensive Guide to Secure Linux Backups Data loss can cripple a Linux system. Accidental deletions, hardware failures, or system corruption necessitate a robust backup strategy. Restic is a leading solution, providing speed, securi

Top 10 Most Popular Linux Distributions in 2025 Entering 2025, we are excited to share with Linux enthusiasts the most popular distribution this year so far. DistroWatch has always been the most reliable source of information about open source operating systems, with particular attention to Linux distributions and BSD versions. It continuously collects and presents a lot of information about Linux distributions, making them easier to access. While it doesn't measure the popularity or usage of a distribution very well, DistroWatch remains the most accepted measure of popularity within the Linux community. It uses page click ranking (PHR) statistics to measure the popularity of Linux distributions among website visitors. [You can

Linux Window Managers: A Comprehensive Guide to the Best Tiling Options Linux window managers orchestrate how application windows behave, quietly managing the visual arrangement of your open programs. This article explores top-tier tiling window man


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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
