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.

  • 3
    I suggest starting with the [Find man page](http://man7.org/linux/man-pages/man1/find.1.html). Then possibly taking a look at [How to run find -exec?](https://unix.stackexchange.com/q/12902/237982). Of course it's probably also a good idea if you make a stop [Here](https://unix.stackexchange.com/q/131766/237982) before writing any shell scripts. – jesse_b Feb 26 '18 at 17:58
  • How about use find with 'size' parameter? It's very simple: find . -size [+-] . – Yurij Goncharuk Feb 26 '18 at 18:10
  • No need recursion AFAIK, if OP is a beginner, I think he should use basic conditions/algorithm, not advanced tools – Gilles Quénot Feb 26 '18 at 18:14
  • @GillesQuenot: I'm almost positive OPs teacher is trying to point him towards `find` with this objective. – jesse_b Feb 26 '18 at 18:21

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
Gilles Quénot
  • 31,569
  • 7
  • 64
  • 82