Solved

BASH Scripting Clear File Contents

Posted on 2010-11-15
4
394 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
ID: 34135426
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
ID: 34136069
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
ID: 34136085
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
ID: 34195520
Many thanks for your help
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Hide vba in gp 7 82
Modify a small python script 19 96
route 2 traffic streams on single NIC 6 35
Q2. How to run DAX query from SSMS. I don’t see any DAX query type. 1 47
This article will show, step by step, how to integrate R code into a R Sweave document
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

911 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

20 Experts available now in Live!

Get 1:1 Help Now