Solved

Delete files with 0 bytes

Posted on 2004-09-20
10
1,148 Views
Last Modified: 2010-04-21
Hi,

I'm running a Datastage jobs that create 10 files.I have to write a shell script that will find all the files created at that time range ie as soon the script start running, and delete all the files with 0 bytes.

Your answer is highly appreciated.
Thanks
Srinivas
0
Comment
Question by:srpendyala
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 40

Expert Comment

by:jlevie
ID: 12104349
find /path-to/dir -type f -empty -exec rm {} \;

Should do it.
0
 
LVL 20

Expert Comment

by:tfewster
ID: 12105718
> ie as soon the script start running...

That sounds a bit risky - What if the job creates the file but doesn't write anything to it for a few seconds? The "delete" task might remove it in that time and you'd lose real data.

What is the problem with a few 0-byte files existing until the Datastage job completes? You could run jlevies command safely then.
0
 
LVL 48

Accepted Solution

by:
Tintin earned 125 total points
ID: 12108341
The -empty option is not available on all versions of find.  A more portable way is

find /path-to/dir -type f -size 0 -exec rm {} \;

0
 
LVL 40

Expert Comment

by:jlevie
ID: 12108405
Quite true...
0
 

Author Comment

by:srpendyala
ID: 12112950
Thanks for suggestions and answer."tfewster" you are right. appreciated.

I have one more problem when trying to delete files just by using rm   it prompts for -----644  mode ? (y/n) -----
My task is to find all empty files and remove them.So when I'm using following  script

find . -size 0|rm ---    I'm getting the message ---- Usage: rm [-Rfir] file ...

Pls help me in the script which find empty files and rm them.

Thanks in advance
srn
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 40

Expert Comment

by:jlevie
ID: 12115548
The prompt is a result of the file protection mode (644, or rw-r--r--) and the user running the command isn't the owner of the files. Either run the rm command as the user that owns the files or as root.

"find . -size 0|rm" isn't the command that Tintin posted. Try exactly what's in his comment.
0
 
LVL 20

Expert Comment

by:tfewster
ID: 12115550
It looks like the files are owned by another userid and have permissions 644 - so your userid can't modify them.  However, if you have write permissions on the _directory_, you can delete the files - you'll just get the warning that they are read-only (to your id).

If you use the -f option to rm, you won't get the warning.

By the way, `rm` doesn't accept input from stdin, so you can't pipe filenames to it in the way you were trying; Use Tintins command, with the -f option, i.e.
find /path-to/dir -type f -size 0 -exec rm -f {} \;
0
 
LVL 6

Expert Comment

by:Talmash
ID: 12134094
not one line but much saftier

foreach file (`find . -type f`)
if (-z $file) then
rm $file
endif
end

tal
0
 
LVL 48

Expert Comment

by:Tintin
ID: 12138283
Talmash.

Please explain how your script (BTW, needs a #!/bin/csh at the top) is "safer" .
0
 
LVL 6

Expert Comment

by:Talmash
ID: 12153884
just no warnings ,

and I am using it for wipeout for the last 5 years .

I am sure there is SHORTER / NICE way to write it .
I just copy-paste what i know is working for me .

about files that are been used at the moment , I can't protect this .

tal
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
create user in TSM 7 590
help pulling data string using Awk 9 63
restricting types of files/data that could be uploaded to Windows & UNIX servers 5 59
UNIX SCP 5 67
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Introduction Regular patching is part of a system administrator's tasks. However, many patches require that the system be in single-user mode before they can be installed. A cluster patch in particular can take quite a while to apply if the machine…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now