Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to empty a file?

Status
Not open for further replies.
Mar 31, 2004
151
US
How could a file be cleaned without opening it? i.e file content needs to be removed but file still remains. Thanks in advance
 
In shell script:
>/path/to/file2empty

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Exactly the same:
>/path/to/file2empty

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
cp /dev/null /my/file/is/empty/now

Regards
-- Franz
Sorry I'm not a native spaeker, I'm from Munich, Germany - "Home of the Whopper", oh no, "Home of the Oktoberfest" ;-)
Solaris System Manager; I used to work for Sun Microsystems Support (EMEA) for 5 years
 
Technically, both those ways still open the file.

If you really need to not open the file, you could create an empty file (e.g. [tt]touch empty[/tt]), then use mv to rename it to the file you want to replace (e.g. [tt]mv empty file-to-clean[/tt]).

 
@chipper

yes, you are right, BUT if you have an open filehandle on "file-to-clean" and you move another file "over" it, what happens to the filehandle?

Regards
-- Franz
Sorry I'm not a native spaeker, I'm from Munich, Germany - "Home of the Whopper", oh no, "Home of the Oktoberfest" ;-)
Solaris System Manager; I used to work for Sun Microsystems Support (EMEA) for 5 years
 
I'm not sure. I assume it would be OS-dependant, and probably not very pretty in any case. It could cause an error in the kernel (though hopefully most OSs can deal with situations like that), it could generate a segmentation fault or something similarly horrible in an I/O call on that file handle, or it might just cause an I/O routine return an error code. In the latter case, what happens next depends on the implementation of the program in question. If it correctly checks for error codes, it might exit gracefully or recover; otherwise, it'd probably just crash.

The original poster would have to evaluate whether or not that would be an issue in his situation if he decided he wanted or needed to use the [tt]mv[/tt] solution.


I assume both of the other presented means of clearing the a file are more than sufficient for whatever is required, but I was just pointing out another in case the "without opening it" was relevant and meant to be taken literally.
 
CD to the directory of the file!

Then type:

cat /dev/null > filename.txt

This will remove all of the data from "filename.txt"

WORKS FOR ALL FILE EXTENSIONS!

[ .txt .php .isg .html .shtml ] etc...
 
> if you have an open filehandle on "file-to-clean" and you move another file "over" it, what happens to the filehandle?

On Unix systems nothing will happen to the file handle. It will still point to the original file. However, the directory entry will now be pointing to a new file. So any programs opening the file will get the "new" file. And when the file handle is closed the original file will be deleted (as there are not directories entries pointing to it).
 
that's what I expected; a lot of my customers mv'ed Files and created a new logfile without stopping the application, next they wondered why the application "stopped" logging...

Regards
-- Franz
Sorry I'm not a native spaeker, I'm from Munich, Germany - "Home of the Whopper", oh no, "Home of the Oktoberfest" ;-)
Solaris System Manager; I used to work for Sun Microsystems Support (EMEA) for 5 years
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top