File is not empty?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File is not empty?
# 1  
Old 04-05-2002
Question File is not empty?

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

Something like:

if [ !-z $filename ]
then


?????
# 2  
Old 04-05-2002
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.
# 3  
Old 04-06-2002
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.
Jimbo
# 4  
Old 04-08-2002
Computer thank you

I appreciate your help. I tested with the -s and it worked great.
# 5  
Old 05-07-2004
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!
# 6  
Old 05-07-2004
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.
# 7  
Old 05-07-2004
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to reformat output if input file is empty, but not if file has data in it

The below awk improved bu @MadeInGermany, works great as long as the input file has data in it in the below format: input chrX 25031028 25031925 chrX:25031028-25031925 ARX 631 18 chrX 25031028 25031925 chrX:25031028-25031925 ARX 632 14... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

if/else with an empty file...

Hi there, I'm just starting out with shell scripting and I'm trying to make a test where the script will basically run yum check-update to find out if the server has any available and if it does it e-mails me but if not it will just stop there. I have it working if there are actually... (7 Replies)
Discussion started by: Imnewtothis
7 Replies

3. Shell Programming and Scripting

Empty out the file

Hi All, I have a piece of perl code in which I DON'T want to delete a file rather empty out the contents, here is the code - if ( unlink("$opt_b") == 1 ) { print_log( "$opt_b deleted", 1 ); }else { print_log( "Could not delete $opt_b:$!", 1 ); ... (5 Replies)
Discussion started by: jacki
5 Replies

4. Shell Programming and Scripting

while file is empty

how can i use while loop ? while file is empty do.... if not empty do ..... in bash (1 Reply)
Discussion started by: Trump
1 Replies

5. UNIX for Dummies Questions & Answers

Getting same exit status for empty and non empty file

Hi All, I am checking for a empty input file to do some further action , but I am getting exit status 0 in both the cases , for empty and non empty file both. The value of $? is coming 0 in if part also and else part too. #!/bin/ksh if ]; then echo "data" # exit 0 echo "$?" else... (4 Replies)
Discussion started by: mavesum
4 Replies

6. Shell Programming and Scripting

how to see if the file is empty or not?

hi how can I determine, if a file is empty or not?I am using read line clause. The script should be like: while read line do if(file is empty) then; ...... done < $blacklist (1 Reply)
Discussion started by: tjay83
1 Replies

7. UNIX for Dummies Questions & Answers

Trying to empty file using > but the file size increasing when next append

AIX 5.3 / KSH I have a Java application which creates a log file a.log. I have a KSH script which does the following action cp a.log /directory2/b.log > a.log After this the file size goes to 0 as per "ls -l" Then next time when the application writes into this file, the file size... (4 Replies)
Discussion started by: firdousamir
4 Replies

8. UNIX for Dummies Questions & Answers

how to find empty folders without using -empty

hi all: my solaris FIND does not support find myFolder -type d -empty how can i find the empty folders? thanks! (7 Replies)
Discussion started by: lasse
7 Replies

9. Shell Programming and Scripting

How to empty a file(already created)

What is the command to empty an already existing file. please provide me.i used Touch cmd to empty the file.but it changing the time only. (4 Replies)
Discussion started by: laknar
4 Replies

10. UNIX for Advanced & Expert Users

empty file in hp-ux

Hi, I need your help. How can I create an empty filename with a specific size, in hp-ux? Regards, Mizi (2 Replies)
Discussion started by: Mizi
2 Replies
Login or Register to Ask a Question