Monday, June 25, 2007

How to Truncate a file in Unix/Linux

:>filename

If the filename already exists,the file will be truncated to ZERO byte file else a new file with ZERO byte will be created.

Example:

[vjsujay@phenix vjsujay]$ ls -l jsk
-rw-r----- 1 vjsujay free 46 Jun 25 20:14 jsk

[vjsujay@phenix vjsujay]$ : > jsk

[vjsujay@phenix vjsujay]$ ls -l jsk
-rw-r----- 1 vjsujay free 0 Jun 25 20:15 jsk

One more example with 3 different methods:

volcano@volcano-laptop:~/test/trunc$ ls -ltr
total 12
-rw-r--r-- 1 volcano volcano 14 2011-02-23 11:56 rado
-rw-r--r-- 1 volcano volcano 14 2011-02-23 11:57 radoo
-rw-r--r-- 1 volcano volcano 14 2011-02-23 11:57 radooo
volcano@volcano-laptop:~/test/trunc$ > rado
volcano@volcano-laptop:~/test/trunc$ :>radoo
volcano@volcano-laptop:~/test/trunc$ true > radooo
volcano@volcano-laptop:~/test/trunc$ ls -ltr
total 0
-rw-r--r-- 1 volcano volcano 0 2011-02-23 11:57 rado
-rw-r--r-- 1 volcano volcano 0 2011-02-23 11:57 radoo
-rw-r--r-- 1 volcano volcano 0 2011-02-23 11:57 radooo

No comments: