Home >System Tutorial >LINUX >How To Quickly Empty Files Without Deleting Them In Linux And Unix
Today, we'll learn how to clear the contents of a file from command line without deleting the file itself in Linux and Unix-like operating systems. Emptying files in Linux can be useful if you want to preserve the file's permissions and ownership but remove all its contents.
Table of Contents
Before we learn how to quickly empty a file without deleting it, let's understand why and when this is useful.
Clearing the contents of a file without deleting the file itself can be important for several reasons, often related to system administration, logging, and application management:
Regardless of the reason, if you've ever wanted to clear a file's content without deleting it in Linux, here are a few ways to do it.
The following 7 methods will clear the contents of your file without deleting the file itself.
1. Using > Redirection:
The > operator in Linux and Unix is used for redirection. When used with a file name, it redirects the output of a command to the file. If no command is given, it effectively redirects "nothing" to the file, thus clearing its contents. It's a straightforward method to empty file content in Linux, keeping the file itself intact.
> file.txt
Replace file.txt with your own.
2. Using the truncate Command:
The truncate command is specifically designed to change the size of a file.
To clear the contents of a file with truncate command, run:
truncate -s 0 file.txt
By setting the size to 0 bytes using the -s 0 option, you effectively remove all content from the file without touching the file's existence or its metadata, like permissions and ownership.
3. Using echo Command with Redirection:
The echo command is typically used to display a line of text/string that is passed as an argument. When combined with the -n flag (which prevents the newline character from being added) and redirection (>) to a file, it replaces the file's content with nothing, thereby emptying the file. This method is as simple as using direct redirection but involves a command (echo).
echo -n > file.txt
4. Using the cp Command with /dev/null:
The cp command is used to copy files or directories. By copying /dev/null to a file, you're essentially replacing the file's contents with the content of /dev/null (which is always empty), thereby clearing the file.
cp /dev/null file.txt
This method is useful for scripts and programs that require the file to remain accessible with the same permissions and ownership.
5. Using sed Command:
To delete all the lines from the file named file.txt using sed command, run:
sed -i d file.txt
Here's what each part of the command does:
6. Using Vim Editor:
You can delete a large file content using vim editor.
Open the file using vim editor:
vim file.txt
Press Esc button and type the following:
:1,$d
Typing :1,$d in Vim executes a command to delete all lines in the entire file. Here's a breakdown of what each part of the command does:
So, :1,$d tells Vim to delete all lines from the first line to the last line of the file. After executing this command, the file will be empty.
7. Using the dd Command:
The dd command is a low-level data copying utility that can copy and convert data between files, devices, or partitions.
By copying data from /dev/null to the file (of=file.txt), you are effectively writing zero bytes to the file, clearing its contents. It's a bit overkill for this task but is useful for its versatility.
dd if=/dev/null of=file.txt
In this tutorial, we discussed five different command line methods to empty the contents of a file without deleting the file itself in Linux. These commands allows you to maintain the file's permissions, ownership, and symbolic links while resetting its contents.
Whether you're managing log files, resetting configuration files, or simply need to clear a file for another use, these tools provide an easy and quick way to do it.
The above is the detailed content of How To Quickly Empty Files Without Deleting Them In Linux And Unix. For more information, please follow other related articles on the PHP Chinese website!