File is not empty?

How do I check to make sure a file is not zero bytes?

Something like:

if [ !-z $filename ]
then

?????

The test command is your best bet.

Look at the bottom -z is for zero test and -n is for non-zero test.

man test
test(1) test(1)
NAME
test - condition evaluation command
SYNOPSIS
test expr
[ expr ]
DESCRIPTION
The test command evaluates the expression expr and, if its value is
True, returns a zero (true) exit status; otherwise, a nonzero (false)
exit status is returned. test also returns a nonzero exit status if
there are no arguments. The following primitives are used to
construct expr:
-r file True if file exists and is readable.
-w file True if file exists and is writable.
-x file True if file exists and is executable.
-f file True if file exists and is a regular file.
-d file True if file exists and is a directory.
-c file True if file exists and is a character special
file.
-b file True if file exists and is a block special file.
-p file True if file exists and is a named pipe (fifo).
-u file True if file exists and its set-user-ID bit is
set.
-g file True if file exists and its set-group-ID bit is
set.

       -k file        True if file exists and its sticky bit is set.

       -s file        True if file exists and has a size greater than
                      zero.

       -h file        True if file exists and is a symbolic link.

       -t [fildes]    True if the open file whose file descriptor number is fildes \(1 by default\) is associated with a terminal device.

       -z s1          True if the length of string s1 is zero.


       -n s1          True if the length of the string s1 is non-zero.

lesstjm is asking how to make sure a file is not zero bytes. I think he wants to test actual file content, in which case he will want the -s option. But since the sample code used a variable, that leaves the door open to the other interpretation of wanting to test content of the variable instead of the file it references.

So just to clarify, -s will test the file itself for content, and -z and -n will test the contents of the variable.

I appreciate your help. I tested with the -s and it worked great.

How can I test if a file is opened by a process?

I have the following situation: a process (suppose I don't know anything about it) writes data into a file very very slowly. I want to copy the file to another location ONLY after the process closes the file (and I want to be very sure about that).

I've tried to check the file size from time to time but this method doesn't always work. I've also tried with
test -N file
where N file is true if file exists and was modified since last read
but I can't figure out how this works.

Can anyone give a clue?
Thanks!

Not sure if fuser will work on all flavours of UNIX.

This will loop until the file is copied. It will check every 60 seconds to see if the file is still locked and copy it when it's unlocked.

I haven't tested this

#!/bin/ksh

Status=1

while [ $Status -ne 0 ]
do
sleep 60

ProcID=\`/usr/sbin/fuser filename | awk -F: '\{print $2\}'\`

if [ "$ProcID" = "" ] 
then 
    cp filename somewhere
fi
Status = $?

done

########################

Matt.

look into useing lsof.

i have a process that can take about 1 to 2 hours to pump out 1 small file and i had the same issues so i went with lsof.

we can use losf for this problem .if using this command we can find out whoever opened the wich files also we can find based on the username and login anme etc..

eg:

lsof -c names -u ^user

the above command used for show the files are opened by processess whose names startes with "names", but exclude those whose owner is the user "user".

we can have more options in lsof command (-t,nl,-u,-c..etc)

Thanks,
Siva.P
Bangalore