0

I have a little introductory exercise to do as homework, I have to do a little script which takes 2 arguments, first argument a directory the second argument a number.

The functionality is to classify the files from the directory into 2 subdirectories. First subdirectory for files larger than the second argument and the second subdirectory for smaller files.

My thought were to take $5 from ls -l but i think its not a good idea, can someone give me any recommendation? Thank you.

1 Answers1

2

As you stated, don't parse ls output.

You can check file size with stat -c '%s' file (bytes) in a for loop. As a starter :

#!/bin/bash

cd "$1"

for file in *; do
    # code/tests here on each "$file"
done

Then you can use bash arithmetic to do some conditions on file size.

Test by yourself to implement this, and feel free to edit (and/or comment) your post if you have some problems with code


Another solution (from comments), use with the -size switch if you remember your teacher had talked of this tool, ex :

find "$1" -size +100

Check

man find | less +/-size