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

1 comment:

Patrick Co Eban said...


Networking Projects for Final Year CSE Students


The IEEE Network projects Networking Projects for Final Year CSE Students has direct impact on undergraduate and graduate student education and training. Final Year Engineering Students who are software developers can structure a project around building a network firewall application Final Year Project Centers in Chennai

JavaScript Training in Chennai

JavaScript Training in Chennai