I need to create an empty file using a shell script. The filename will be determined by examining all the files in a directory matching a pattern, say TEST.*
, and appending _END
to it so that the new file might look like TEST.2011_END
. Let the current directory be $DIR
. So I tried:
for file in $DIR/TEST.*
do
touch $file_END
done
This gives an error. I also tried replacing touch $file_END
with:
filename=$file_END
touch $filename
but with no luck. What should I do?
touch
is often used to create an empty file, but its primary purpose it to touch a file's timestamp... If a file with the name already exist, 'touch' will not make it become "empty".... If you want to force an existing file to be empty (thereby losing any data it contains), you can usecp /dev/null "${file}_END"
which will either create an empty file if it doesn't currently exist, or if it does exist, it will truncate it to zero length (ie. empty)... – Peter.O Apr 01 '11 at 15:42: >"${file}_END"
– Gilles 'SO- stop being evil' Apr 01 '11 at 21:22