How to Empty a File in a bash Shell Script

Posted on 2010-11-15
Last Modified: 2012-05-10
The code is inside a loop that redirects output to a file. That file is then tested for whether it's empty or not. So, at the beginning of each iteration of the loop, I want to empty the file.

Can you tell me how to do that?

Something like this would be nice:

   for ... do
      $FILE < EOF
      Other stuff...
      command 2> $FILE
      if $FILE not empty
          do something
Question by:WizeOwl
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
LVL 26

Accepted Solution

wilcoxon earned 350 total points
ID: 34141658
To empty a file, you can just do:

cp /dev/null $FILE

Assisted Solution

sandipshah earned 50 total points
ID: 34141667
Or delete the file and re-open it with the same name.

LVL 12

Assisted Solution

tel2 earned 100 total points
ID: 34141683
Or simply:

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

LVL 84

Expert Comment

ID: 34141689
command 2> $FILE
will empty the file before it writes

Author Comment

ID: 34141739
Thanks for all the suggestions.

1) cp /dev/null $FILE   >> is probably closest to what I was looking for.
2) Brute force is to delete and let first redirect "2>" create it when needed, again.
3) If ">$FILE" works, that may be most elegant.
4) command 2> $FILE doesn't help when testing loop iterations.
LVL 84

Expert Comment

ID: 34141773
command 2> $FILE doesn't help when testing loop iterations.
what are you testing that it doesn't help?

Author Closing Comment

ID: 34141784
>$FILE does work

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (,  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

707 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