

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Empty or Delete a Large File Content in Linux?
Usually a continuously growing files, needs to be emptied from time to time to accept the latest data from the next operation. There are various mechanisms to empty the file. We will see them one by one below. The common approach is the overwrite the target file by using > sign which should come from a source which is empty.
/dev/null
This is a common method where we output empty result and then redirect that result to the target file.
# Original file size $ls-lt -rw-rw-r-- 1 ubuntu ubuntu 2925 Jan 1 08:39 ref_file.txt # Redirect the output from /dev/null $ cat /dev/null > ref_file.txt -rw-rw-r-- 1 ubuntu ubuntu 0 Jan 1 09:35 ref_file.txt
echo
The echo command with null out put can also make the target file empty.
$ls-lt -rw-rw-r-- 1 ubuntu ubuntu 2925 Jan 1 08:39 ref_file.txt $echo > ref_file.txt $ls –lt -rw-rw-r-- 1 ubuntu ubuntu 0 Jan 1 09:36 ref_file.txt
Using >
Just putting only a > just before the file name will make it empty. We can also use just a colon(:) to get the same result.
$ls-lt -rw-rw-r-- 1 ubuntu ubuntu 2925 Jan 1 08:39 ref_file.txt $ > ref_file.txt $ls –lt -rw-rw-r-- 1 ubuntu ubuntu 0 Jan 1 09:39 ref_file.txt
Using truncate
Using truncate command with the size option as 0 will make the target file empty.
$ truncate s -0 ref_file.txt $ ls-lt -rw-rw-r-- 1 ubuntu ubuntu 0 Jan 1 09:41 ref_file.txt
Using touch
$ touch ref_file.txt $ ls-lt -rw-rw-r-- 1 ubuntu ubuntu 0 Jan 1 09:45 ref_file.txt
- Related Questions & Answers
- How to split or break large files into pieces in Linux?
- How to disable delete permission of File and Directory in Linux?
- Java Program to delete file or directory
- How to change file or directory permission in Linux/Unix?
- How to copy a file, group of files, or directory in Linux?
- How to shrink or extend the size of a file in Linux?
- Delete the file or directory in Java
- How to list the directory content in Linux?
- How to delete a file using Python?
- How to delete a temporary file in Java?
- Delete file or directory on termination in Java
- How to convert the content of the file into uppercase or lowercase?
- How to sort a file in-place in Linux?
- Delete a file in C#
- C program to delete a file
Advertisements