Hi everyone. I am fairly new to Linux and I am taking a UNIX course. I have been asked to write a shell script and I am having a little trouble. I was wondering if anyone could help me. Here are my instructions --

create a script called command find_big.sh that recursively finds file equals or exceed X bytes and outputs the names to stdout. The shell shall take 2 arguments: 1 the location to start looking; 2 the size to equal or exceed. Example bash find_big.sh /lib 20000.
---Hint you shell should begin
#!/bin/bash
#validate there are 2 arguments passed
#validate argument 1 exists and display an error if it does not
#validate argument 2 is a number and display error if no


And this is all I've come up with so far.

#!/bin/bash
#validate there are 2 arguments passed
#validate argument 1 exists and display an error if it does not
#validate argument 2 is a number and display error if not
directory=$1
size=$5
ls -l $1 | sort -n -k 5 | awk '{if($5>=size){print $9$5}}'

My if statement in awk is obviously not working. The if statements confuse me some (I have yet to make one work). I think I am trying to make this harder than it is. What I have so far will list any directory sorted by file size, but I'm not sure how to pull our the files that are greater than or equal to my second argument and just list them. I was hoping to make the listing part of this work first then go back and insert the argument validations. I've just gotten myself more confused though. I seem to learn better through reverse engineering for some reason.

If anyone could help, I'd be grateful.

Thanks,

Tim Cummings
tim.cummings@cox.net
Oklahoma City :?:

Ok, I've gotten myself further now, but I'm still a little stuck. I have this done so far:

#!/bin/bash
#validate there are 2 arguments passed
#validate argument 1 exists and display an error if it does not
#validate argument 2 is a number and display error if not
directory=$1
size=$2
ls -l -R $1 | sort -n -k 5 | awk '{print $9$5}'
if test -d $1; then directory=$1; else echo "Directory invalid"
fi
if test $size -ge 0; then size=$2; else echo "Specify a file size"
fi


So basically I list the directory and if either argument is not there the script displays an error at the end, but how can I pull the file size out of the listing and only display those files with the specified sizes from argument 2?

Hello,

grep or sed might have some functionality you are looking for. Also consider ls -al instead of ls -l because you will miss anything in a hidden directory (a .directory) if you are just in the -l mode.

This is a doozie of a project. Too sleepy to think right now. Grr.

Christian

Thanks for your input. I think I'm dancing all around it. I've put grep in between my sort and awk commands, but I can't get the format down. I think it should be something like grep -r if $size>=$2, but that somehow screws up my second argument because then I get the error "grep: 5000: No such file or diretory" when I type ./find_big.sh /boot 5000 at the command line. I've tried switching it around to be $2>=$size and a few other things. I think this statement is all I lack from making this script successful, but I just can't make my brain see it. This is due by Sunday and I think I'm starting to panic a little. I don't usually take this long in figuring something out. I'm just frustrated with this, but at the same timeI want to learn this because I reallize how important programming can be. We move on to Perl next week and I'm afraid if I don't get this I might miss out on something in Perl.

I got it figured out another (simpler) way, although I would still like help figuring my original way out. What I ended up with was:

#!/bin/bash
#validate there are 2 arguments passed
#validate argument 1 exists and display an error if it does not
#validate argument 2 is a number and display error if not
directory=$1
size=$2
find $1 -size +$2c
if test -d $1; then directory=$1; else echo "Directory invalid"
fi
if test $size -ge 0; then size=$2; else echo "Specify a file size"
fi

Hello Tim,

Great work on solving it. Yes, find would work too. I should have thought of that. Damn.

Great work!

Christian

#!/bin/bash
#validate there are 2 arguments passed
#validate argument 1 exists and display an error if it does not
#validate argument 2 is a number and display error if not
directory=$1
size=$2
if test -d $1; then directory=$1; else echo "Directory invalid"
fi
if test $size -ge 0; then size=$2; else echo "Specify a file size"
fi
find $1 -size $2c -o $2c

I forgot to add the part that makes it list the exact size and anything greater. :(
I also switched the test to happen first to validate my arguments before running find.

Hello Tim,

Great work on solving it. Yes, find would work too. I should have thought of that. Damn.

Great work!

Christian

Thanks Christian. I beat myself over the head repeatedly when I saw the easier solution. Man I don't know why I always make this harder than it needs to be.
:o

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.