LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Truncating files in directory (https://www.linuxquestions.org/questions/linux-newbie-8/truncating-files-in-directory-689259/)

Swapna173 12-09-2008 03:40 AM

Truncating files in directory
 
I truncated the files in one of the directory with .log extension.

I wrote a script something like this:

filecount=`ls -1 *.log 2>/dev/null | wc -l`

if [ $filecount -gt 0 ]
then
for i in `ls -1 *.log`
do
echo > $i
done
fi

Here, when it all the files perfectly except the last file. For the last file, instead of truncating it , it creates a new files with same name appended with ? at the end of the file name.

How can overcome this problem. is there something wrong with my script.

unSpawn 12-09-2008 05:07 AM

I can not reproduce it, but people should use 'find' for finding files instead of trying to list them. With 'find' you also don't need the "filecount" check and it doesn't break with spaces (IFS) like 'ls' does. You could also check the ABS about the difference between "for" and "while" loops. So. How about using 'find /some/path -maxdepth 1 -type f -iname \*.log -print0|xargs -0 -iX echo truncate 'X'' instead?


All times are GMT -5. The time now is 10:22 AM.