Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all the content from file

Tags:

ruby

I want to remove the content form the list of files.

EDIT :

I have list of files.

file1 file2 file3

Those file containing bunch of lines ...

I want to remove all the lines from each files.

like image 563
krunal shah Avatar asked Sep 28 '10 18:09

krunal shah


People also ask

How do I delete all contents of a file in vi?

Immediately after opening a file, type “gg” to move the cursor to the first line of the file, assuming it is not already there. Then type dG to delete all the lines or text in it.

How do you clear the contents of a file in Python?

Use the truncate() Function to Clear the Contents of a File in Python. The truncate() method in the Python file handling allows us to set the size of the current file to a specific number of bytes. We can pass the desired size to the function as arguments. To truncate a file, we need to open it in append or read mode.


2 Answers

Looks like you need to truncate file. Do something like:

File.open('/tmp/file', 'w') {|file| file.truncate(0) } 
like image 83
taro Avatar answered Oct 31 '22 13:10

taro


To truncate a file:

File.truncate('/path/to/file', 0) 

To truncate list of files:

[file1, file2, file3].each { |file| File.truncate(file, 0) } 
like image 24
Tamer Shlash Avatar answered Oct 31 '22 13:10

Tamer Shlash