Wrong output in find command

Hi guys -

I am trying a small script to tell me if there is a file that exists less than 1k. It should report ERROR, otherwise the check is good.

I wrote this script down, however it never runs in the if/then statement. It always returns the echo ERROR.

MYSIZE=$(find /home/student/dir1 -type f -name '*' -size -1k)
if [ $? -eq 1 ]
then
echo "No Files Less Than 1k"
else
echo "ERROR: Found files less than 1k:
${MYSIZE}"
fi


Try this -

#/bin/ksh
#set -vx

for fname in `find . -type f -size -1024c`
do
        echo $fname
done

But how do i define directory path?

Also I am looking for it to either say "none" or "displays the file location and the file in question"

Also 1024c is also displaying files that have a 1kb size.

You can find more information using the man find command for its usage.

Refer the '.' by your 'path'. In your example,

find /home/student/dir1 -type f -1024c

You can change the size to 1023c in the command.

same error. not working

I think you need to actually run MYSIZE, otherwise you are checking the exit code of previous command (setting the MYSIZE variable), which always succeeds. Please correct me anyone if I'm wrong.

pseudocoder is correct.

Dallas,

Try this, it worked for me. This one considers the files in all the sub-directory level too.

#/bin/ksh
#set -vx

typeset -i fsize
typeset -i smallfiles
typeset -i bigfiles
smallfiles=0
bigfiles=0

for fname in `find . -type f`
do
        line=`ls -l $fname| awk '{print $5" "$9}'`
        fsize=`echo $line|cut -f1 -d" "`
        fname=`echo $line|cut -f2 -d" "`
        if [[ $fsize -lt 1024 ]]
        then
                smallfiles=$((smallfiles + 1))
        else
                bigfiles=$((bigfiles + 1))
        fi
done
echo "Small files #"$smallfiles
echo "Big files #"$bigfiles

Alternative solution, which gets the count at the directory level (not sub-directory's files)

#/bin/ksh
#set -vx

        ls -l | awk '
        BEGIN {
                smallfile=0
                bigfile=0
        }
        NF == 9 && /^-/ {
                if ($5 < 1024 ){
                ++smallfile}
                else{
                ++bigfile}
                }
        END {
        print "Small files #", smallfile, " Big files #", bigfile
        }'

No, he's not. (Sorry, pseudocoder ;)) Well, partially correct. Correct about having to use MYSIZE but incorrect about the exit status.

Note that $? is equal to the exit status of the subshell, which is equal to the last command run in that subshell (tested in bash and ksh):

$ var=$(false; true; exit 45)value
$ echo Results: \$\?=$? \$var=$var
Results: $?=45 $var=value

After that first statement, the value of $? is the exit status of the last command executed by the subshell, find, and not that of the variable assignment. The problem is that the original poster's code is improperly interpreting the exit status of find. find will return 0 whether or not it finds anything, as long as no error occurred. The echo error branch is always running because no errors are occurring and the exit status is always 0.

Instead of...

if [ $? -eq 1 ]

... use ...

if [ ! "$MYSIZE" ]

... or, if you flip the then and else clauses, simply ...

if [ "$MYSIZE" ]

Regards,
Alister

You are correct Alister :slight_smile:
interesting confusing thread :wink: