Wrong output in find command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Wrong output in find command
# 1  
Old 05-03-2010
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.

Code:
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

# 2  
Old 05-03-2010
Try this -
Code:
#/bin/ksh
#set -vx

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

# 3  
Old 05-03-2010
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.
# 4  
Old 05-03-2010
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

Quote:
Also 1024c is also displaying files that have a 1kb size.
You can change the size to 1023c in the command.

Last edited by NPRao; 05-03-2010 at 05:01 PM..
# 5  
Old 05-03-2010
same error. not working
# 6  
Old 05-03-2010
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.
# 7  
Old 05-03-2010
pseudocoder is correct.

Dallas,

Try this, it worked for me. This one considers the files in all the sub-directory level too.
Code:
#/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)
Code:
#/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
        }'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

If && command giving wrong output

Hi All, I am trying to run a script which will search for 2 strings(stopped,started) in a text file and echo an output depending on below condition -bash-3.2$ cat trial1.txt v ggg f -bash-3.2$ cat trial1.sh VAR9=` grep 'stopped' /tmp/trial1.txt` VAR10=` grep 'started'... (4 Replies)
Discussion started by: srkmish
4 Replies

2. Shell Programming and Scripting

Output of find command to variable?

Hi, I'd like to assign the output of the find command to a variable. What I need is to run the find command, and if it returns zero files, the program exits. so i'm trying to assign the output of the find command to the $var1 variable....and then if this is less than one, I echo a... (2 Replies)
Discussion started by: horhif
2 Replies

3. Shell Programming and Scripting

Grep command showing wrong output in Linux

Hi All I am trying to run a script in linux wherein i have a command like this grep ^prmAttunityUser= djpHewr2XFMAttunitySetup_ae1_tmp djpHewr2XFMAttunitySetup_ae1_tmp is a temporary file in which the user value is stored but this command in the script returns me balnk value whereas it has a... (4 Replies)
Discussion started by: vee_789
4 Replies

4. Shell Programming and Scripting

Check my script and correct the find command if wrong

Hello All, Here I am trying to find all the directories whose name starts with EFS or HOTFIX like in below example and below is my code but I don’t know why this is not working correctly. drwxr-xr-x 3 qabuild denccefs 4096 Sep 23 21:36 EFS110929A_SOURCE/ -rwxrwxr-x 1 qabuild... (2 Replies)
Discussion started by: anuragpgtgerman
2 Replies

5. Emergency UNIX and Linux Support

getting wrong output with AWK command!!!

i have a file which gets appended with 9 records daily and the file keeps growing from then...i use to store the previous day files count in a variable called oldfilecount and current files count as newfilecount.my requirement is that i need to start processing only the new records from the... (3 Replies)
Discussion started by: ganesh_248
3 Replies

6. UNIX for Advanced & Expert Users

what is wrong with this find command

i am trying to find the files which are more than 100MB and it was created 10 days ago. find /lola/loaded -size +102400 -mtime -10 -print | xargs ls -ltr -rw-rw-r-- 1 lola_adm gdrmp 82054170 Jun 23 06:17 /lola/loaded/ILMEMBER20090622.txt -rw-rw-r-- 1 lola_adm gdrmp 652080494 Jun 24... (3 Replies)
Discussion started by: sudhiroracle
3 Replies

7. Shell Programming and Scripting

tr command giving wrong output

Hi All, i have a file which have many fields delimited by ,(comma) now i have to show only few fields and not all. the sample text file looks like this: TYPE=SERVICEEVENT, TIMESTAMP=05/06/2009 11:01:40 PM, HOST=sppwa634, APPLICATION=ASComp, FUNCTION=LimitsService, SOU... (8 Replies)
Discussion started by: usha rao
8 Replies

8. Shell Programming and Scripting

Sort command giving wrong output

Hi all, I have a problem with sort command. i have a file which looks like this: "file1 1073 java/4 1073 java/180 1073 java/170 1073 java/176 1073 java/167 1073 java/40 1073 java/33 1073 java/136 28988 java/76 28988 java/73 28988 java/48 28988 java/26" and i want to sort... (8 Replies)
Discussion started by: usha rao
8 Replies

9. UNIX for Dummies Questions & Answers

problem with output of find command being input to basename command...

Hi, I am triying to make sure that there exists only one file with the pattern abc* in path /path/. This directory is having many huge files. If there is only one file then I have to take its complete name only to use furter in my script. I am planning to do like this: if ; then... (2 Replies)
Discussion started by: new_learner
2 Replies

10. UNIX for Dummies Questions & Answers

output of find command

Hi, I am confused about the output of find command. Please see the two find commands below. When i put "*.c" i get lots of files. But when i put *c only i get only one file. Any answer?? $ find . -name "*c" ./clarify/cheval/hp_server/rulemanager/rulemansvc... (3 Replies)
Discussion started by: shriashishpatil
3 Replies
Login or Register to Ask a Question