Solved

BASH Scripting Clear File Contents

Posted on 2010-11-15
4
387 Views
Last Modified: 2012-05-10
I would like to know how to clear a files contents using a BASH script. It is important that it only clears the contents incase they need to be accessed again so I can't do "rm".

I am using this to find and list the files:

find ./ -name "*.log" -type f -mtime +1 -exec ls {} \;
0
Comment
Question by:Lico_w
  • 2
4 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
Comment Utility
find ./ -name "*.log" -type f -mtime +1 | while read file ; do >$file; done

"-exec" or even "| xargs" can't be used here because of the ">" sign, so we need a loop.

0
 
LVL 14

Expert Comment

by:Deepak Kosaraju
Comment Utility
this can also be done in followin way
find ./ -name "*.log" -type f -mtime +1 -exec cat /dev/null > {} \;
0
 
LVL 14

Expert Comment

by:Deepak Kosaraju
Comment Utility
oh sorry I never knew we cannot use > when we use exec r xargs. May be follow @woolmilkporc - I never the above in person its command formation I normally do on fly.
0
 

Author Closing Comment

by:Lico_w
Comment Utility
Many thanks for your help
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now