how to test for file existence using file size?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to test for file existence using file size?
# 1  
Old 10-26-2005
how to test for file existence using file size?

ok im doing this in a normal shell. i need to check the file in the script. how should i write the if else statment?


if the filesize contains 0kb then it will echo out file contains nothing
else if the script contains more than 0kb then it will echo out file exist.


any one care to help? thanks ! im new to unix scripting..
# 2  
Old 10-26-2005
Code:
if [ -s /path/to/file ]; then 
echo file contains something
else echo contains nothing
fi

Try checking the man pages on your system as well. In this case, man test.
# 3  
Old 10-26-2005
thank you blowtorch!
# 4  
Old 10-26-2005
Here's something a little more informative, if you need it, but it's in ksh. Dunno if that's normal enough for you. -e is not available in sh:

#!/bin/ksh
if [ -s /path/to/file ] ; then
echo We have a file with stuff in it
elif [ -e /path/to/file ] ; then
echo We have a file with 0-length
else
echo "The file isn't even there!"
fi

do a "man test" on a UNIX box near you.

BTW, if you're on Linux then /sbin/sh is likely really bash, and you can use the -e.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Renaming file and check for the renamed file existence

Hi Am trying to move a file from one name to another When I do "ls" to check for the moved filename I can see the file but when I try the same with a script am unable.. I think am doing some pretty silly error.. please help.. toMove=`ls | grep -E "partition.+"` mv $toMove partition._org... (7 Replies)
Discussion started by: Priya Amaresh
7 Replies

2. Shell Programming and Scripting

Script to check for the file existence, if file exists it should echo the no of modified days

Hi, I am looking for a shell script with the following. 1. It should check whether a particular file exists in a location #!/bin/sh if ; then echo "xxx.txt File Exists" else echo "File Not Found" fi 2. If file exists, it should check for the modified date and run a command... (2 Replies)
Discussion started by: karthikeyan_mac
2 Replies

3. Solaris

How to test the existence of trailer record

SunOS 5.10 Generic_142900-15 sun4v sparc SUNW,T5240 I have a script that needs to test a file for the existence of a trailer record. Is there a command and is a header and trailer differect record type? Thanks in advance (1 Reply)
Discussion started by: Harleyrci
1 Replies

4. UNIX for Dummies Questions & Answers

Checking Existence and file size of File

Hi, I am a SAP- ABAP programer , i have only little knowledge of Unix. My issue is i have a list of files which are in internal table. I want to create a script which consist of all these files and in one go i can check in the unix server about there existance and there file size. Can... (2 Replies)
Discussion started by: amitkumar.b2
2 Replies

5. Shell Programming and Scripting

Test for existence of files

Hello, Can you please help me to see if log files exist in a directory? I need to scan logs in different directories, so I am using an array to change dynamically. I need help in the if test statement dir=/logs/MSD dir=/logs/UPD countA=1 while (( countA <= ${#dir } )) do cd ${dir}... (1 Reply)
Discussion started by: drbiloukos
1 Replies

6. Shell Programming and Scripting

Parse file from remote server to calculate count of string existence in that file

Hi I need to parse the file of same name which exist on different servers and calculate the count of string existed in both files. Say a file abc.log exist on 2 servers. I want to search for string "test" on both files and calculate the total count of search string's existence. For... (6 Replies)
Discussion started by: poweroflinux
6 Replies

7. Shell Programming and Scripting

Test File for Existence with Whitespaces in Path

Hi Everyone! I'm quite new to shell scripting so this might be trivial, though 3 days of struggle and search didn't help to solve the problem: I want to look for files called '*HUN*' in a huge amount of directories most of their names contain whitespaces and print the path of the directory if... (8 Replies)
Discussion started by: sumi76
8 Replies

8. Shell Programming and Scripting

Test for a file existence

deleted (1 Reply)
Discussion started by: ust
1 Replies

9. Shell Programming and Scripting

Multiple file existence and checking file size

I want to check the files in particular directory are more that 0 Bytes i.e, Non zero byte file. The script should print a msg if all the files in that directory are empty( 0 Byte). (2 Replies)
Discussion started by: lathish
2 Replies

10. UNIX for Dummies Questions & Answers

How to test for a specific file size

Hello, In my shell program, I need to test for a specific size of a text file before it can be imported into an oracle table. If the size is less than that number, my program should stop processing. What is the correct command to do this test? Thanks! (1 Reply)
Discussion started by: GEBRAUN
1 Replies
Login or Register to Ask a Question