Solved

BASH Scripting Clear File Contents

Posted on 2010-11-15
4
401 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

808 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