Visit Our UNIX and Linux User Community


Checking file sizes in script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Checking file sizes in script
# 1  
Old 07-30-2009
Checking file sizes in script

Hi,

I'm trying to check a filesize within a script and then excute a relevant action. An example is below:

Code:
if [ -f $filename2 ]

   then rm $filename
          rm $filename2

elif [ ! -f $filename2 ]

    then rm $filename2

fi

Basically if $filename2 has a filesize of 0 then I want both files to be removed, but if the filesize is greater then 0 I only want the one file removed. Unfortunately when I run the above script all files are removed regardless of the filesize of $filename2.

Any ideas?

Thanks

Chris

Use CODE tags when posting code, data or logs to enhance readability and to preserve formatting like indention etc., ty.

Last edited by zaxxon; 07-30-2009 at 07:27 AM.. Reason: code tags
# 2  
Old 07-30-2009
Code:
if [ -s $filename2 ]
then
rm $filename2
else
rm $filename $filename2
fi

# 3  
Old 07-30-2009
use -s flag instead of -f

Code:
if [ ! -s $filename2 ]

then
rm $filename
rm $filename2

else

rm $filename2

fi

# 4  
Old 07-30-2009
Thanks guys.

What's the difference between -f and -s? and why does -s work and not -f?
# 5  
Old 07-30-2009
Code:
-f

    file is a regular file (not a directory or device file)
-s

    file is not zero size

# 6  
Old 07-30-2009
Or:

Code:
if [ ! -s "${filename2}" ]
then
        rm "${filename}"
fi
rm "${filename2}"

# 7  
Old 07-30-2009
Hammer & Screwdriver Commonly used test file operators

To answer your question about -f and -s, here is a partial list of the operators that can be used:

Code:
-d  file is directory
-e  file exists
-f  file is a normal file
-r  file can be read (surprising how often a process cannot read because of permission issues)
-s  file is greater than 0 bytes
-w  file can be written to (see note on -r)
-x  file is executable (useful when calling another script)

 

Previous Thread | Next Thread
Test Your Knowledge in Computers #552
Difficulty: Medium
If variable i has the value 10, after x = 5 + (++i); the value of x after this code is 15.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

File checking script need help

Hi, Gurus, I need a scripts to check specified file if it exists or not at certain time (say every month between 5th and 7th). if file exists do something otherwise do another thing. can anybody help this? Thanks in advance :wall: (3 Replies)
Discussion started by: ken002
3 Replies

2. UNIX for Dummies Questions & Answers

Checking for file Sizes

Hi , I have some 10 files where i need to check the size of each and every file...if the size of the file is 0...I shud send out an email mentioning which file is actually of 0KB size.. Pls help (13 Replies)
Discussion started by: saggiboy10
13 Replies

3. Shell Programming and Scripting

Script to compare file sizes

I need to write a bash script larger X Y that compares the sizes of two specified files X and Y, and reports which file is larger. For example, if X is larger, the output should be "File X is larger", while if Y is larger, the output should be "File Y is larger". If the files are exactly the... (3 Replies)
Discussion started by: julia_21436
3 Replies

4. Shell Programming and Scripting

Help with script checking for a file in various servers

I am trying to write a script that checks whether or not, a file exists on multiple servers. My code / logic so far is: #!/usr/bin/ksh print "Enter File name to be checked" read MYFILE ssh server1 " cd /var/opt/logs ; if then ... (4 Replies)
Discussion started by: momin
4 Replies

5. Shell Programming and Scripting

Script check for file, alert if not there, and continue checking until file arrives

All, Is there a way to keep checking for a file over and over again in the same script for an interval of time? Ie If { mail -user continue checking until file arrives file arrives tasks exit I don't want the script to run each time and email the user each time a file... (4 Replies)
Discussion started by: markdjones82
4 Replies

6. Programming

Checking columns in SQL, comparing user input and sizes.

I'm writing a KSH shell script that's using SQL though DB2. If I have a table defined and populated db2 "create table tb(num int,letter char(4))" db2 "insert into tb values(111,a) db2 "insert into tb values(112,b) db2 "insert into tb values(111,c) How can I check if a letter user... (0 Replies)
Discussion started by: busdude
0 Replies

7. Shell Programming and Scripting

Script for checking and reporting file sizes in a directory.

Hi, Need help for a Script for checking and reporting database file sizes in a directory. Request you to please give your valuable inputs. Thanks a lot in advance. Best Regards, Marconi (1 Reply)
Discussion started by: marconi
1 Replies

8. Shell Programming and Scripting

Script to check and report database file sizes...

Need help for a script to check and report database file sizes. (2 Replies)
Discussion started by: marconi
2 Replies

9. Shell Programming and Scripting

Simple file checking script

Hi, I have a really, what I hope is, simple question. I'm looking for a simple way to see whether a file exists or not and then perform an action based on whether it exists or not. An example of what I tried is as follows: if then { echo "File mysql exists" ... (1 Reply)
Discussion started by: _Spare_Ribs_
1 Replies

10. Shell Programming and Scripting

Script for file names/sizes

How can I look at a certain directory and list all the file names, locations and sizes of each file in the current directory and all subdirectories? (2 Replies)
Discussion started by: ssmiths001
2 Replies

Featured Tech Videos