How to empty file contents w/o rm and touch


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to empty file contents w/o rm and touch
# 1  
Old 07-18-2007
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
# 2  
Old 07-18-2007
Code:
echo "" > file 
cp /dev/null file
dd if=/dev/zero of=output.file bs=1 count=1

# 3  
Old 07-18-2007
Code:
cat /dev/null > your_file

lorcan
# 4  
Old 07-18-2007
and this,
Code:
>filename

# 5  
Old 07-18-2007
Quote:
Originally Posted by matrixmadhan
and this,
Code:
>filename



does this really work??? where and how to use this at the shell prompt??
# 6  
Old 07-18-2007
Quote:
Originally Posted by kris_kris
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.
# 7  
Old 07-18-2007
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 09:47 AM.. Reason: Display comment of error message in 'cat'
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Creating multiple empty files with touch

Hello, sorry to bother anyone reading this I have an assignment with a question that reads: Your current directory is stenton. Create empty files called f1, f2, and f12 (in that order), within stenton So my first thought was to enter: touch f1 f2 f12 but that does not work, does anyone... (1 Reply)
Discussion started by: eleuin
1 Replies

2. 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

3. 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

4. Shell Programming and Scripting

check if contents of an array is empty

hi, I have this array a=("" "" "") Is there a command that I can use to check if all members of the are empty? I really don't like to use for or while loop. thanks! ---------- Post updated at 02:23 PM ---------- Previous update was at 02:15 PM ---------- wait, I got it... (1 Reply)
Discussion started by: h0ujun
1 Replies

5. Shell Programming and Scripting

I want to delete the contents of a file which are matching with contents of other file

Hi, I want to delete the contents of a file which are matching with contents of other file in shell scripting. Ex. file1 sheel,sumit,1,2,3,4,5,6,7,8 sumit,rana,2,3,4,5,6,7,8,9 grade,pass,2,3,4,5,6,232,1,1 name,sur,33,1,4,12,3,5,6,8 sheel,pass,2,3,4,5,6,232,1,1 File2... (3 Replies)
Discussion started by: ranasheel2000
3 Replies

6. Shell Programming and Scripting

Empty Multiple files contents

I would like to empty multiple files contents (without delete the file) which have similar name to begin with. e.g. log1.txt log2.txt log3.txt log4.txt I know cat /dev/null will do the job but that only apply to a single file. But when i tried cat /dev/null > {} \; that doesnt do... (7 Replies)
Discussion started by: kin
7 Replies

7. 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

8. UNIX for Dummies Questions & Answers

Touch all files and subdirectories (recursive touch)

I have a folder with many subdirectories and i need to set the modified date to today for everything in it. Please help, thanks! I tried something i found online, find . -print0 | xargs -r0 touch but I got the error: xargs: illegal option -- r (5 Replies)
Discussion started by: glev2005
5 Replies

9. UNIX for Dummies Questions & Answers

Getting same exit status for empty and non empty file

Hi All, I am checking for a empty input file to do some further action , but I am getting exit status 0 in both the cases , for empty and non empty file both. The value of $? is coming 0 in if part also and else part too. #!/bin/ksh if ]; then echo "data" # exit 0 echo "$?" else... (4 Replies)
Discussion started by: mavesum
4 Replies

10. Shell Programming and Scripting

Creating file contents using contents of another file

Hi, I am not sure how to start doing this so I hope to get some advice as to how to start. I have 2 files. The source file contains data that I needed is in columns delimited by ";". For example, in this format: "CONTINENT","COUNTRY","CITY","ID" "asia","japan","tokyo","123"... (21 Replies)
Discussion started by: ReV
21 Replies
Login or Register to Ask a Question