LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-17-2011, 12:16 PM   #1
tractng
Member
 
Registered: Nov 2007
Posts: 75

Rep: Reputation: 15
Command to remove contents of file


What is the command to remove the contents of the file while it is active.

Let's say I have a file called test.log file. I want to remove the contents of the file. OS is Centos/Redhat.

TIA

tractng
 
Old 08-17-2011, 12:23 PM   #2
nerak99
Member
 
Registered: Feb 2006
Distribution: FC 15
Posts: 42

Rep: Reputation: 2
rm myfile.abc

(will remove the file)
then

touch myfile.abc

to put an empty copy there

OR

vi myfile.abc

This will open the file in vi editor were "d" will delete lines and provided the file is not too big, holding down the d key will delete all the lines. Then type

:w

to write the empty file
 
Old 08-17-2011, 12:54 PM   #3
tractng
Member
 
Registered: Nov 2007
Posts: 75

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by nerak99 View Post
rm myfile.abc

(will remove the file)
then

touch myfile.abc

to put an empty copy there

OR

vi myfile.abc

This will open the file in vi editor were "d" will delete lines and provided the file is not too big, holding down the d key will delete all the lines. Then type

:w

to write the empty file
Thanks for the response. I am aware of those commands. Is there one command that can just empty the contents of the file without removing the file or going into it?
 
Old 08-17-2011, 12:59 PM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,130
Blog Entries: 2

Rep: Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825Reputation: 4825
Code:
>test.log
should do the job.
 
Old 08-17-2011, 01:01 PM   #5
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,261

Rep: Reputation: 297Reputation: 297Reputation: 297
Hi

You can also redirect "nothing" into it:

>myfile.abc

Keep in mind that programs keeping the file open for writing will need a restart. Otherwise the log still goes into the deleted file.

Edit: Slow typer...
 
Old 08-17-2011, 04:01 PM   #6
tractng
Member
 
Registered: Nov 2007
Posts: 75

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by TobiSGD View Post
Code:
>test.log
should do the job.

That is it. Just couldn't remember. Thanks.
 
Old 08-17-2011, 04:45 PM   #7
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 3,774
Blog Entries: 1

Rep: Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339Reputation: 1339
If you are looking to clear a log file,.. you might want to look into "logrotate" which automatically archives old logs... clears the way for new logs, etc...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Inserting file contents into Sed Command ewingtux Programming 4 11-19-2007 07:59 PM
Any command to view contents of CD image (iso file) ? lawrence_lee_lee Linux - Software 3 10-10-2007 01:38 AM
Bash remove part of a file based on contents of another file bhepdogg Programming 4 01-31-2007 04:13 PM
file contents to command line args hawkmauk Linux - General 3 11-21-2006 02:27 PM
Read contents of file and execute command cosiek Programming 3 09-30-2003 12:07 PM


All times are GMT -5. The time now is 07:07 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration