# Emptying files in a loop

**URL:** <https://community.unix.com/t/emptying-files-in-a-loop/267620>\
**Category:** Shell Programming and Scripting\
**Created:** [June 14, 2010, 11:40am UTC](https://community.unix.com/t/emptying-files-in-a-loop/267620 "2010-06-14T11:40:14Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![SixSigma1978](https://community.unix.com/letter_avatar/sixsigma1978/32/5_5575768a8748004e209b776fc1b2916d.png) [@SixSigma1978](https://community.unix.com/u/SixSigma1978)\
**Post date:** [June 14, 2010, 11:40am UTC](https://community.unix.com/t/emptying-files-in-a-loop/267620/1 "2010-06-14T11:40:14Z")

</div>

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 :

```nohighlight
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

---

<div class="post-metadata">

**Author:** ![vbe](https://community.unix.com/user_avatar/community.unix.com/vbe/32/266_2.png) [@vbe](https://community.unix.com/u/vbe)\
**Post date:** [June 14, 2010, 11:56am UTC](https://community.unix.com/t/emptying-files-in-a-loop/267620/2 "2010-06-14T11:56:00Z")

</div>

It should work:

```nohighlight
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:

```nohighlight
FILES=$(ls *.log)

```

Or its equivalent for bash...

---

<div class="post-metadata">

**Author:** ![SixSigma1978](https://community.unix.com/letter_avatar/sixsigma1978/32/5_5575768a8748004e209b776fc1b2916d.png) [@SixSigma1978](https://community.unix.com/u/SixSigma1978)\
**Post date:** [June 14, 2010, 11:59am UTC](https://community.unix.com/t/emptying-files-in-a-loop/267620/3 "2010-06-14T11:59:46Z")

</div>

Changed it to

```nohighlight
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?

---

<div class="post-metadata">

**Author:** ![vbe](https://community.unix.com/user_avatar/community.unix.com/vbe/32/266_2.png) [@vbe](https://community.unix.com/u/vbe)\
**Post date:** [June 14, 2010, 12:25pm UTC](https://community.unix.com/t/emptying-files-in-a-loop/267620/4 "2010-06-14T12:25:21Z")

</div>

AS I said Im no bash specialist... but I would try:

```nohighlight
export FILES="$(ls *.log)"
...

```

---

<div class="post-metadata">

**Author:** ![dr.house](https://community.unix.com/letter_avatar/dr.house/32/5_5575768a8748004e209b776fc1b2916d.png) [@dr.house](https://community.unix.com/u/dr.house)\
**Post date:** [June 14, 2010, 12:31pm UTC](https://community.unix.com/t/emptying-files-in-a-loop/267620/5 "2010-06-14T12:31:13Z")

</div>

> [@sixsigma1978](#):
>
> ```plaintext
> FILES=$(ls *.log)
> for f in $FILES
> 
> ```

How about ...

```plaintext
for FILE in $( ls *.log )

```

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

---

<div class="post-metadata">

**Author:** ![alister](https://community.unix.com/letter_avatar/alister/32/5_5575768a8748004e209b776fc1b2916d.png) [@alister](https://community.unix.com/u/alister)\
**Post date:** [June 14, 2010, 12:35pm UTC](https://community.unix.com/t/emptying-files-in-a-loop/267620/6 "2010-06-14T12:35:29Z")

</div>

> [@sixsigma1978](#):
>
> ```plaintext
> 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

---

<div class="post-metadata">

**Author:** ![SixSigma1978](https://community.unix.com/letter_avatar/sixsigma1978/32/5_5575768a8748004e209b776fc1b2916d.png) [@SixSigma1978](https://community.unix.com/u/SixSigma1978)\
**Post date:** [June 14, 2010, 12:39pm UTC](https://community.unix.com/t/emptying-files-in-a-loop/267620/7 "2010-06-14T12:39:20Z")

</div>

> [@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!!! :b:

Thanks all!!!
