Linux empty a file

2 points
Created by:

Quick solution:

cp /dev/null my_filename.txt

Practical example.

In below example we empty the tomcat logs.


  • Before the file size is 636M
  • We executed the command to empty the file cp /dev/null catalina.out
  • After the file size is 0

Below we have an example from linux command line.

Before - file size 636M:

root@local:/opt/tomcat/logs# ls -alh
-rw-r-----  1 root root 636M Jan 14 20:22 catalina.out

Execute command:

root@local:/opt/tomcat/logs# cp /dev/null catalina.out

After - file size 0:

root@local:/opt/tomcat/logs# ls -alh
-rw-r-----  1 root root   0 Jan 14 20:23 catalina.out

Other solutions

1. It is posible to redirect empty string into file.

echo -n > my_filename.txt

2. dev-null redirection gives same result

cat /dev/null > my_filename.txt
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.


Native Advertising
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂