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