Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Search Forums:



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-18-2007
Registered User
 

Join Date: Jul 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
How to empty file contents w/o rm and touch

Hi all,

Can any one tell me how to empty the contents of a file in unix without using the following sequence : rm <file> ; touch <file>

is there any command in unix??
is it possible through shell scripting? If so, how?

-krishna
Sponsored Links
    #2  
Old 07-18-2007
sysgate's Avatar
sysgate sysgate is offline Forum Advisor  
Unix based
 

Join Date: Nov 2006
Location: Bulgaria
Posts: 1,419
Thanks: 0
Thanked 6 Times in 6 Posts

Code:
echo "" > file 
cp /dev/null file
dd if=/dev/zero of=output.file bs=1 count=1

Sponsored Links
    #3  
Old 07-18-2007
Registered User
 

Join Date: May 2007
Posts: 220
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
cat /dev/null > your_file

lorcan
    #4  
Old 07-18-2007
Technorati Master
 

Join Date: Mar 2005
Location: classification algos
Posts: 3,174
Thanks: 17
Thanked 26 Times in 24 Posts
and this,

Code:
>filename

Sponsored Links
    #5  
Old 07-18-2007
Registered User
 

Join Date: Jul 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by matrixmadhan View Post
and this,

Code:
>filename



does this really work??? where and how to use this at the shell prompt??
Sponsored Links
    #6  
Old 07-18-2007
Registered User
 

Join Date: May 2007
Posts: 220
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by kris_kris View Post
does this really work??? where and how to use this at the shell prompt??
I think this works only with ksh/bash shell not in csh. you just have to enter the command in your prompt.
Sponsored Links
    #7  
Old 07-18-2007
Shell_Life's Avatar
Registered User
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 1,203
Thanks: 1
Thanked 100 Times in 97 Posts
Here are other ways not mentioned yet:

Code:
: > file
cat file > file      ## Gives error message in ksh, but it works.
head file > file
tail file > file


Last edited by Shell_Life; 07-18-2007 at 08:47 AM.. Reason: Display comment of error message in 'cat'
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
search for the contents in many file and print that file using shell script cdfd123 Shell Programming and Scripting 3 10-07-2007 10:17 PM
File is not empty? lesstjm Shell Programming and Scripting 7 07-12-2007 10:21 AM
empty file in hp-ux Mizi UNIX for Advanced & Expert Users 2 08-09-2006 06:29 AM
Creating file contents using contents of another file ReV Shell Programming and Scripting 21 02-24-2006 09:25 AM
command 'touch -c file/dir' zp523444 UNIX for Advanced & Expert Users 1 11-20-2005 01:11 PM



All times are GMT -4. The time now is 05:07 AM.