Emptying files in a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Emptying files in a loop
# 1  
Old 06-14-2010
Emptying files in a loop

I'm trying to write a script that will empty all contents in a set of files in a directory - without deleting the files themselves.
I know the command (at BASH prompt) is: > (filename)

I'm iterating over all files in the directory and calling the > command thus :

Code:
FILES=*.log
for f in "$FILES"
do
        echo "Emptying file "$f
        > $f
        echo "Emptied file "$f
done

However, this doesn't seem to be doing anything - what am I missing?

Jim
# 2  
Old 06-14-2010
It should work:
Code:
civ:/datanfs/data $ echo $F;ll $F;>$F;ll $F
ls_out.txt
-rwxr-x---   1 vbe        bin           1039 Jun 14 17:48 ls_out.txt
-rwxr-x---   1 vbe        bin              0 Jun 14 17:49 ls_out.txt



---------- Post updated at 17:56 ---------- Previous update was at 17:52 ----------

Second thought: Its maybe your script - but a guess since I use ksh and have no bash to test... I suspect the FILES=*...
It should be:
Code:
FILES=$(ls *.log)

Or its equivalent for bash...
# 3  
Old 06-14-2010
Changed it to

Code:
FILES=$(ls *.log)
for f in $FILES
do
        >"The file : $f"
done

It throws
syntax error at line 1: `FILES=$' unexpected

this is odd since
FILES=$(ls *.log) works at the bash prompt
but not inside a .sh file!!!! Any idea why this could be happening?

Last edited by SixSigma1978; 06-14-2010 at 01:19 PM..
# 4  
Old 06-14-2010
AS I said Im no bash specialist... but I would try:
Code:
export FILES="$(ls *.log)"
...

# 5  
Old 06-14-2010
Quote:
Originally Posted by SixSigma1978
Code:
FILES=$(ls *.log)
for f in $FILES

How about ...

Code:
for FILE in $( ls *.log )

This works in Bash. (Not sure about the sh shell you're mentioning, though ...)

Last edited by dr.house; 06-14-2010 at 01:33 PM.. Reason: remark added
# 6  
Old 06-14-2010
Quote:
Originally Posted by SixSigma1978
Code:
FILES=*.log
for f in "$FILES"
do
        echo "Emptying file "$f
        > $f
        echo "Emptied file "$f
done

However, this doesn't seem to be doing anything - what am I missing?

Jim
That code is fine, just don't quote "$FILES". The double quotes prevent file globbing from occuring, so "$FILES" expands to "*.log" and that's it. The loop will execute once and try to truncate a file named *.log. Without the quotes, $FILES expands to *.log which is then expanded further into a list of all files that match the pattern.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 7  
Old 06-14-2010
Quote:
Originally Posted by alister
That code is fine, just don't quote "$FILES". The double quotes prevent file globbing from occuring, so "$FILES" expands to "*.log" and that's it. The loop will execute once and try to truncate a file named *.log. Without the quotes, $FILES expands to *.log which is then expanded further into a list of all files that match the pattern.

Regards,
Alister
Alister - that did it. Tho am pretty sure everyone was pretty much right in the end as well!! Sucks to be newb!!! Smilie

Thanks all!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Loop through files in a directory

Hello How do I loop through files in a specific directory ? This script is not working! #! /bin/bash FILES=/usr/desktop/input/* For f in $FILES; do awk '-v A="$a" -v B="$b" {$6=($1-64)/2 ;$7=((10^($6/10))/A)^(1/B) ; print}' OFS="\t" $f > /root/Desktop/output/$f.txt; done ... (7 Replies)
Discussion started by: ali.seifaddini
7 Replies

2. Shell Programming and Scripting

Emptying the history

is there any other way for someone to see the history of my commands after i've nulled the .bash_history file? i'm curious. i usually do this each time i want to prevent spies: cat /dev/null > .bash_history i work in an environment where multiple people have root access. meaning, we can... (1 Reply)
Discussion started by: SkySmart
1 Replies

3. Shell Programming and Scripting

Sed emptying file when i use for search replace operation

Hi Friends I am new to sed programming , i found that the below code can search for the $ToSearch and Replace it with $ToReplace ( $ToSearch and $ToReplace are my variables in my script ) sed "s/$ToSearch/$ToReplace/" $file > $output mv $output $file In testing the script i found that... (3 Replies)
Discussion started by: rakeshkumar
3 Replies

4. UNIX for Dummies Questions & Answers

Emptying a file (bring the size of the file down to 0 , or close to 0)

Hi I tried to empty an existing file (bring the size of the file down to 0). When I used “> myFile” or “cat </dev/null >myFile”, when I do a “ls –la”, the file size shows as 0. I then wrote 540 lines to the file, and then opened it using vi, I see something like this: "myFile" 540 lines,... (3 Replies)
Discussion started by: qmqmqm
3 Replies

5. AIX

Impacts of emptying /var/adm/wtmp file ?

In our operating procedures, if a workstation has a space problem in the /var filesystem, one of the most frequent case we were told is the size of the /var/adm/wtmp file. Someone once told me it is dangerous to do this. Is it ? I cannot say for certain that whomever wrote that procedure is... (2 Replies)
Discussion started by: Browser_ice
2 Replies

6. Shell Programming and Scripting

loop through files in directory

hi all i have some files present in a directory i want to loop through all the files in the directory each time i loop i should change the in_file parameter in the control file and load it into a table using sql loader there is only one table where i have to load alll the files ... (3 Replies)
Discussion started by: rajesh_tns
3 Replies

7. UNIX for Dummies Questions & Answers

For loop using 2 files

Hi all, Can anyone shed any light on the following problem? I have 2 columns in File1: 10 8 30 44 50 59 94 96 ... (15 Replies)
Discussion started by: dr_sabz
15 Replies

8. Linux

Emptying Trash

Hi all i hope someone can help me, in gnome if you right click on trash, you get another menu appear 'Empty Trash' what i want to do is be able to edit this command so that it secure deletes the trash, where is that command? so i can edit it. thanks in advance for any help, Dave (shred -z -u ) (0 Replies)
Discussion started by: dave123
0 Replies

9. UNIX for Dummies Questions & Answers

Loop through files

Hi, I'm trying loop through all files in a directory that have a filename starting with 'CC', and process them one by one. Can any provide an example of how I could do this. I've started with: if test -f CC* then #add files to an array #loop through array and process the file based on... (1 Reply)
Discussion started by: kshelluser
1 Replies

10. Shell Programming and Scripting

Null values after emptying a log file

Hi, I have a log file which is constantly being written to by some process. I need to clear that log file on a daily basis. The problem is that when I issue this command: echo "" > logfile.log the file gets filled with nulls thus increasing the size of the file. Is there a way to... (2 Replies)
Discussion started by: kasie4life
2 Replies
Login or Register to Ask a Question