Empty Multiple files contents


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Empty Multiple files contents
# 1  
Old 05-13-2011
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 anything at all.
# 2  
Old 05-13-2011
This would fix your problem (I think)

try this thing;
Code:
find . -type f -name '*.txt' -print | while read i
do
  echo " " > $i
done

this will clear out the content of every *.txt found in "." (current directory)

It is not totally clear but a space I think...
hope it help..(smiley)

Last edited by Scott; 05-13-2011 at 08:26 PM.. Reason: Added code tags
This User Gave Thanks to Be_Learned For This Post:
# 3  
Old 05-13-2011
Code:
for i in log*.txt
do
cat /dev/null >"$i"
done

This User Gave Thanks to ctsgnb For This Post:
# 4  
Old 05-13-2011
Zero works aswell Smilie
Code:
.... whateva loop ..
0>$i
.....

This User Gave Thanks to Peasant For This Post:
# 5  
Old 05-13-2011
Quote:
Originally Posted by Peasant
Zero works aswell Smilie
Nothing works as well Smilie
Code:
.... whateva loop ..
>$i
.....

This User Gave Thanks to ctsgnb For This Post:
# 6  
Old 05-14-2011
Code:
echo log*.txt | xargs -n1 cp /dev/null

These 2 Users Gave Thanks to shamrock For This Post:
# 7  
Old 05-14-2011
Code:
for i in log*.txt; do
  printf "" > "$i"
done

This User Gave Thanks to Scrutinizer For This Post:
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. UNIX for Beginners Questions & Answers

Split file into multiple files based on empty lines

I am using below code to split files based on blank lines but it does not work. awk 'BEGIN{i=0}{RS="";}{x="F"++i;}{print > x;}' Your help would be highly appreciated find attachment of sample.txt file (2 Replies)
Discussion started by: imranrasheedamu
2 Replies

3. Shell Programming and Scripting

Check file from multiple files is empty using awk

I am passing multiple files in awk & since one of the file is empty(say file3) so the same gets skipped & logic goes for toss. Need suggestion/help in checking and putting additional checks for the same awk -F, 'FNR==1 {++filecounter} filecounter==1 {KRL=$2;next} filecounter==2... (8 Replies)
Discussion started by: siramitsharma
8 Replies

4. Shell Programming and Scripting

Single command to create multiple empty files(no trailing lines as well).

Hi, i need a single command to create multiple empty files(no trailing lines as well) and empty the files if already existing. please let me know or if this has been ansered, if some ocan share the link please, thanks > newfile.txt or :> newfile.txt do not work (4 Replies)
Discussion started by: Onkar Banerjee
4 Replies

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

6. Shell Programming and Scripting

Empty out multiple files with a single command?

I have a log directory: /logs/foo.log /logs/bar.log /logs/err.out I'm trying to find a way to > /logs/*.log > /logs/*.out to blank them out, but of course, that doesn't work. Any suggestions? (4 Replies)
Discussion started by: Validatorian
4 Replies

7. Shell Programming and Scripting

Shell Script to store contents of multiple files into one

Hi, I"m writing a script to store all the contents of multiple files with different file names into one single file. I am giving in only last modified date of file in a folder. The below script gives a list of just one file based on the input date i give which is taken as string variable. I... (2 Replies)
Discussion started by: ashrocks
2 Replies

8. Shell Programming and Scripting

Rename contents inside multiple files

Hi, I have 100 files in a directory. Each file have the following format >CtbRe01234 fdfjdhfkdfkd >CtL2B0456 gjfgfkgjfkgjfk >CmdrE05768 fghdjskksllfkLike this I have many files in the directory. What I want is; rename the header content in each file such that the above file... (6 Replies)
Discussion started by: Lucky Ali
6 Replies

9. Shell Programming and Scripting

Create Multiple files by reading a input file and changing the contents

Being new to this area .I have been assigned a task which i am unable to do . Can any one please help me . Hi I have requirement where i have input file XYZ_111_999_YYYYMMDD_1.TXT and with header and series of Numbers and Footer. I want to create a mutiple output files with each file having a... (2 Replies)
Discussion started by: bhargavkr
2 Replies

10. Shell Programming and Scripting

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 (7 Replies)
Discussion started by: kris_kris
7 Replies
Login or Register to Ask a Question