empty a file

How To Empty a Large File or Delete Them On Linux 8

There’re lots of commands to delete a file using different methods. Let’s see how to empty or remove a file in Linux. We can also remove or delete the file using those commands in Linux.

We can easily clean or create an empty Linux file. We can also create a bash script to
empty a file. 

1. We can use ‘:‘ is a shell built-in command to empty the file with the below command. I will create a file and try to make it empty. you can also use true > file.log

[[email protected] ~]# fallocate -l 200MB file.log
[[email protected] ~]# : >file.log 
[[email protected] ~]# du -sh file.log 
0 file.log

Check Out: How To Change Default Torrent Program on Ubuntu

2. We can perform these tasks using echo command as shown below.

[[email protected] ~]# fallocate -l 200MB file.log
[[email protected] ~]# echo -n > file.log
or 
[[email protected] ~]# echo -n "" > file.log
[[email protected] ~]# du -sh file.log 
0 file.log

3. We have another way to empty a file with truncate command but this is slower than > filename.

Syntax: truncate -s size filename

[[email protected] ~]# du -sh file.log 
191M file.log
[[email protected] ~]# truncate -s 0 file.log 
[[email protected] ~]# du -sh file.log 
0 file.log

4. We can also use the cp command to empty the file with the null value.

[[email protected] ~]# cp /dev/null file.log 
cp: overwrite 'file.log'? yes
[[email protected] ~]# du -sh file.log 
0 file.log

5. If you need to empty the file without removing it, you can the below commands to do it. Create a file with fallocate command with any size or use the existing file with a large size.

Using ‘>‘ to write the null input to the file.

[[email protected] ~]# fallocate -l 200MB file.log
[[email protected] ~]# du -sh file.log 
191M file.log
[[email protected] ~]# > file.log 
[[email protected] ~]# du -sh file.log 
0 file.log

6. Suppose you have the file with some message written in it. you want to open the file and need to remove them with a single command. So, it can be possible with the below commands.

[[email protected] ~]# vim file.log
:1,$d                      # write these to delete content and press enter

--No lines in buffer--      # you will see this messsage and save the file

Check Out: Install and Configure Apache Zookeeper On CentOS 8

7. Another way of empty the file using the cat command.

[[email protected] ~]# du -sh file.log 
191M file.log
[[email protected] ~]# cat /dev/null > file.log
[[email protected] ~]# du -sh file.log 
0 file.log
8. We can also use the 'dd' command to empty the files. 
[[email protected] ~]# fallocate -l 200MB file.log
[[email protected] ~]# dd if=/dev/null of=file.log
0+0 records in
0+0 records out
0 bytes copied, 0.680689 s, 0.0 kB/s
[[email protected] ~]# du -sh file.log 
0 file.log

We can also use the below command to delete the single file or multiple files. 

Syntax: for file in path-of-file; do > $file; done

[[email protected] ~]# for file in file.log; do > $file; done
[[email protected] ~]# du -sh file.log 
0 file.log

You’re done

LEAVE A REPLY

Please enter your comment!
Please enter your name here