?
Solved

BASH Scripting Clear File Contents

Posted on 2010-11-15
4
Medium Priority
?
409 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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…
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

800 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