LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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, 11:16 AM   #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, 11:23 AM   #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, 11:54 AM   #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, 11:59 AM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

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

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
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, 03: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, 03:45 PM   #7
szboardstretcher
Senior Member
 
Registered: Aug 2006
Location: Detroit, MI
Distribution: GNU/Linux systemd
Posts: 4,278

Rep: Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694Reputation: 1694
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



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 06:59 PM
Any command to view contents of CD image (iso file) ? lawrence_lee_lee Linux - Software 3 10-10-2007 12:38 AM
Bash remove part of a file based on contents of another file bhepdogg Programming 4 01-31-2007 03:13 PM
file contents to command line args hawkmauk Linux - General 3 11-21-2006 01:27 PM
Read contents of file and execute command cosiek Programming 3 09-30-2003 11:07 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:34 PM.

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
Open Source Consulting | Domain Registration