Reputation: 1
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?
Upvotes: 0
Views: 317
Reputation: 77105
#!/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!
Upvotes: 2
Reputation: 51665
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
Upvotes: 0