How to clear contents of a file without deleting it.

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to clear contents of a file without deleting it.
# 1  
Old 08-07-2012
How to clear contents of a file without deleting it.


I have a script which will use an input.txt file as an input file.
I am providing data to this input file in the script and once the script is executed, I want to clear all the contents of this file as during the second time use of this script, I'll be appending the data in this input file.

So basically I want to know a way using which I can just clear all the contents of the file input.txt and make it a blank file. Like one which is just created with no data in it.
Can this be done.

Thanks in advance!
# 2  
Old 08-07-2012
# first use of script, clears file
./script > filename
# second use of script, appends file
./script >> filename

# 3  
Old 08-07-2012
> filename
cat /dev/null > filename
echo "" > filename

# 4  
Old 08-07-2012
Originally Posted by itkamaraj
> filename
cat /dev/null > filename
echo "" > filename

'echo' creates a file with one blank line in it. Not empty.

You don't need cat or /dev/null here.
: > filename

# 5  
Old 08-07-2012
Thank's a Ton guys. It really helped me.
Specially to Corona688... that solution is awesome!!!!

UNIX is amazing and equally amazing are all you guys :-)
# 6  
Old 08-07-2012
This works fine, no need use even nop = : command.
> filename

This method is correct method to clear open log files. Not rm.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Clear contents of specified directories, then return exit status

Hello, this is my first post here. I'm attempting to write a bash shell script to rm the contents of a directory without deleting the directory, specifically in OS X 10.10 . Here's what I have: function clear() { USER="$USER" DIR=$1 rm -rfv /Users/"$USER"/library/$DIR/* } clear... (6 Replies)
Discussion started by: YouNicks
6 Replies

2. UNIX for Dummies Questions & Answers

What option will use for deleting directory with all its contents?

Hi How to completely delete directory with all it contents I try to use rmdir -r but it give error Thanks ---------- Post updated at 03:10 AM ---------- Previous update was at 02:52 AM ---------- Hi all I got the solution for my thread i use mkdir with the option -p Thanks (1 Reply)
Discussion started by: Tauatioti
1 Replies

3. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

Replace partial contents of file with contents read from other file

Hi, I am facing issue while reading data from a file in UNIX. my requirement is to compare two files and for the text pattern matching in the 1st file, replace the contents in second file by the contents of first file from start to the end and write the contents to thrid file. i am able to... (2 Replies)
Discussion started by: seeki
2 Replies

5. UNIX for Dummies Questions & Answers

Script For Deleting Contents of "Live" Log File

In our shop, we have a situation where a log file from our interface engine software has begun maxing out in file size (reaching the 32-bit "2147483647" limit). Currently, the only way to rectify this is to stop the interface and restart it, which generates a new log. Easy enough, but the... (6 Replies)
Discussion started by: rjhjr64
6 Replies

6. UNIX for Dummies Questions & Answers

compare 2 file contents , if same delete 2nd file contents

Give shell script....which takes two file names as input and compares the contents, is both are same delete second file's contents..... I try with "diff"...... but confusion how to use "diff" with if ---else Thanking you (5 Replies)
Discussion started by: krishnampkkm
5 Replies

7. Shell Programming and Scripting

Deleting the contents of a folder older than X hours

Every day a new .zip file is uploaded to a folder and at mid-night the zip file is to be extracted into a /data/ folder, inside a date-named folder. # This should extract the contents of a zip file into the /data/ folder into a date based folder /usr/bin/unzip -a -o... (15 Replies)
Discussion started by: worchyld
15 Replies

8. UNIX for Dummies Questions & Answers

Deleting contents of a UNIX File

Hi ALL, Can anyone help me out. I have unix file, I need to delete the contents of the file. Can any one let me know the command to do this. The file has to be there, but the contents should be deleted. thanks Manas (6 Replies)
Discussion started by: manas6
6 Replies

9. Shell Programming and Scripting

Query:just need to remove the contents of the file without deleting that

Hii, Please suggest me how can i only remove the contents of the file without deleting it. (3 Replies)
Discussion started by: namishtiwari
3 Replies

10. Shell Programming and Scripting

Remove the contents of a file without deleting the file

Hi All, I want to delete the contents of few files which are really huge in size. How the same can be done by using sed. Is there any other alternative to sed. Thanks in advance (10 Replies)
Discussion started by: sumesh.abraham
10 Replies
Login or Register to Ask a Question

Featured Tech Videos