How To Empty a Large File or Delete Them On Linux

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.

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

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

[root@example ~]# fallocate -l 200MB file.log
[root@example ~]# : >file.log 
[root@example ~]# 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.

[root@example ~]# fallocate -l 200MB file.log
[root@example ~]# echo -n > file.log
or 
[root@example ~]# echo -n "" > file.log
[root@example ~]# 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

[root@example ~]# du -sh file.log 
191M file.log
[root@example ~]# truncate -s 0 file.log 
[root@example ~]# du -sh file.log 
0 file.log

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

[root@example ~]# cp /dev/null file.log 
cp: overwrite 'file.log'? yes
[root@example ~]# 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.

[root@example ~]# fallocate -l 200MB file.log
[root@example ~]# du -sh file.log 
191M file.log
[root@example ~]# > file.log 
[root@example ~]# 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.

[root@example ~]# 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.

[root@example ~]# du -sh file.log 
191M file.log
[root@example ~]# cat /dev/null > file.log
[root@example ~]# du -sh file.log 
0 file.log
8. We can also use the 'dd' command to empty the files. 
[root@example ~]# fallocate -l 200MB file.log
[root@example ~]# 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
[root@example ~]# 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

[root@example ~]# for file in file.log; do > $file; done
[root@example ~]# du -sh file.log 
0 file.log

You’re done

Share on:

I'm the founder of Curious Viral. I hope this blog will provide you complete information about Linux Technology & I would like to share my technical knowledge with you which I have learned during this period.

Other Posts You May Like...

Leave a comment