Anil Namde
Anil Namde

Reputation: 6608

unix command to truncate file contents

can someone help me with unix command to truncate the contents of the files in the directory. I am using Cygwin in windows.

Upvotes: 4

Views: 2689

Answers (3)

ghostdog74
ghostdog74

Reputation: 342363

for file in *
do
  >$file
done

Upvotes: 9

David V.
David V.

Reputation: 5708

If you want to truncate a file to keep the n last lines of a file, you can do something like (500 lines in this example)

mv file file.tmp && tail -n 500 file.tmp > file && rm file.tmp

Upvotes: 2

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798606

Just redirect from nowhere:

> somefile.txt

Upvotes: 7

Related Questions