LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-09-2008, 03:40 AM   #1
Swapna173
LQ Newbie
 
Registered: Dec 2007
Posts: 25

Rep: Reputation: 15
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.
 
Old 12-09-2008, 05:07 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,353
Blog Entries: 55

Rep: Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Count the number of files in a directory and sub-directories within that directory soumyajit.haldar Linux - Software 4 03-20-2007 06:22 AM
Truncating the length of files\directors to 31 characters or less daysleeper Linux - General 3 09-27-2006 12:28 AM
Autozipping files from 1 directory & dropping them in other directory ??? amitsharma_26 Linux - Networking 5 10-22-2005 06:09 AM
Automatically Copying files from the ftp directory into the html directory swatward Linux - General 3 04-17-2005 10:55 PM
Samba truncating files to 0 bytes KimVette Linux - Networking 6 01-30-2005 06:42 PM


All times are GMT -5. The time now is 02:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration