0

This script was written by my tutor but I don't understand it. Can someone please explain it to me.

#!/bin/bash
find $1 -size +${2}c -size -${3}c

This script suppose to accept three command line arguments: directory name, minimum file size in bytes and maximum file size in bytes. So when running it, it will be like this:

./script.sh /home/Desktop/file 5000 10000

And then the files size between 5000 and 10000 will be echoed to the screen.

Dose anyone know another way of doing the same?

0

2 Answers 2

2
#!/bin/bash
find $1 -size +${2}c -size -${3}c
    |___|     |_____|       |_____|
      |          |             |
This is the    This is      This is the
first argument the second   third argument
passed while    argument
 running the
  script

find utility syntax is to search specified path for files which can be identified depending on the options chosen.

-size n[ckMGTP]
 True if the file's size, rounded up, in 512-byte blocks is n.  
 If n is followed by a c, then the primary is true if the 
 file's size is n bytes (characters).  

Using + in front of second argument means we are looking for files greater then the number specified. Similarly - means the files to be displayed should be less than the size specified.

By passing three arguments to your script, means we are giving $1 as the path to be searched which in your case is /home/Desktop/file. The second argument defines the condition that files should be greater than the specified argument which is 5000. The final argument is for specifying that the files should be less than the specified size which is 10000.

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

0

This script runs as your teacher says.

Error "find: Invalid argument +c to -size. is becausse you don't inform about second argument to script. Then ${2} has no value and script tries to execute:

find your_path -size +$c -size -$c

You can modify your script in order to check for number or arguments:

#!/bin/bash
EXPECTED_ARGS=3
E_BADARGS=65
HLP_ARG="path min_size max_size"
if [ $# -ne $EXPECTED_ARGS ]
then
  echo "Usage: `basename $0` $HLP_ARG"
  exit $E_BADARGS
fi    

find $1 -size +${2}c -size -${3}c

4 Comments

Why E_BADARGS=65 and not E_USAGE=64?
You are free to select your own exit status code.
Rather than requiring 3 arguments, it is often better to simply set reasonable defaults: find $1 ${2+-size +${2}c} ${3+-size -${3}c}
@WilliamPursell, nice appointment. But just to catch answer requeriments: "This script suppose to accept three command line arguments"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.