• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 445
  • Last Modified:

BASH Scripting Clear File Contents

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
Lico_w
Asked:
Lico_w
  • 2
1 Solution
 
woolmilkporcCommented:
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
 
Deepak KosarajuDevOps EngineerCommented:
this can also be done in followin way
find ./ -name "*.log" -type f -mtime +1 -exec cat /dev/null > {} \;
0
 
Deepak KosarajuDevOps EngineerCommented:
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
 
Lico_wAuthor Commented:
Many thanks for your help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now