Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove null byte files 2

Status
Not open for further replies.

7280

MIS
Apr 29, 2003
331
IT
Hi,
I have an application that reads lot of files during the day. Some times these files are null, so the application fails.
I need to create a script that deletes these null files. Unfortunately file names contain space so I'm having problem when using for or if.
To see file space usage, I use wc -c.
An example of filename is: ASDAD 12313 dadf.msg
Files reside in different directories, but all under the same structure.
I tried with someting like this:
find . -name *.msg -exec wc -c {} \;
this returns two colums:
bytes files
980 ./elabdir/aa/dcmtconnection/25e 10f5166 -775d.msg
0 ./elabdir/ab/dcmtconnection/13e 2f51678 -835d.msg
but now I need to use if and for to check if bytes=0 then remove. how can I do this?

Thanks in advance.
 
Thanks all.
I also found the solution, but it's less elegant than yours :)

find . -name *.msg -exec wc -c {} \; | while read a b c d;
do
if [ $a -eq 0 ];
then rm -f "$b $c $d";
fi;
done

Thanks again.
 
Hi

You do not need c and d :
Code:
find . -name *.msg -exec wc -c {} \; | while read a b;
do
  if [ $a -eq 0 ];    
    then  rm -f "$b";  
  fi;
done
At least in my [tt]bash[/tt] and (pd)[tt]ksh[/tt].

Feherke.
 
for me it doesnt' work. it splits file name.
I receive error: ls: 0653-341 The file filename does not exist.
I'm using bash...
 
Hi

It works for me.
Code:
[blue]master #[/blue] ls -l
-rw-r--r--    1 master   users           0 2006-12-07 11:38 a
-rw-r--r--    1 master   users           0 2006-12-07 11:38 b c
-rw-r--r--    1 master   users           0 2006-12-07 11:39 d e f
-rw-r--r--    1 master   users          49 2006-11-03 19:44 first.txt
-rw-r--r--    1 master   users          15 2006-11-03 19:43 second.txt

[blue]master #[/blue] find . -type f -exec wc -c {} \; | while read a b; do   if [ $a -eq 0 ];         then  echo rm -f "$b";     fi; done
rm -f ./a
rm -f ./b c
rm -f ./d e f

[blue]master #[/blue] find . -type f -exec wc -c {} \; | while read a b; do   if [ $a -eq 0 ];         then  rm -f "$b";     fi; done

[blue]master #[/blue] ls -l
-rw-r--r--    1 master   users          49 2006-11-03 19:44 first.txt
-rw-r--r--    1 master   users          15 2006-11-03 19:43 second.txt

[blue]master #[/blue] $SHELL --version
GNU bash, version 2.05b.0(1)-release (i586-suse-linux)
Copyright (C) 2002 Free Software Foundation, Inc.
7280 said:
I receive error: ls: 0653-341 The file filename does not exist.
Where you have [tt]ls[/tt] in that code ?

Feherke.
 
root@bw-docsrv1: /root/scripts/test # ls -las
total 4
0 drwxrwxr-x 2 root system 256 Dec 07 12:11 .
4 drwx------ 6 root system 4096 Dec 07 12:11 ..
0 -rw-rw-r-- 1 root system 0 Dec 07 12:11 aass 23423 bbb.msg
0 -rw-rw-r-- 1 root system 0 Dec 07 12:11 abbb 234 b.msg
0 -rw-rw-r-- 1 root system 0 Dec 07 12:11 abbb 23423 bbb.msg
root@bw-docsrv1: /root/scripts/test # find . -name "*.msg" -exec wc -c {} \; | while read a b
> do
> if [ $a -eq 0 ];
> then
> ls $b
> fi
> done
ls: 0653-341 The file ./aass does not exist.
ls: 0653-341 The file 23423 does not exist.
ls: 0653-341 The file bbb.msg does not exist.
ls: 0653-341 The file ./abbb does not exist.
ls: 0653-341 The file 23423 does not exist.
ls: 0653-341 The file bbb.msg does not exist.
ls: 0653-341 The file ./abbb does not exist.
ls: 0653-341 The file 234 does not exist.
ls: 0653-341 The file b.msg does not exist.
 
Hi

[ol]
[li]Do not make file deletion tests as root ! Not even using [tt]ls[/tt].[/li]
[li]Pay attention to the details, like double quotes ( " ) :
Code:
find . -name *.msg -exec wc -c {} \; | while read a b;
do
  if [ $a -eq 0 ];
    then  rm -f [red][b]"[/b][/red]$b[red][b]"[/b][/red];
  fi;
done
[/li]
[/ol]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top