Move files by its size
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
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.
bash shell scripting
add a comment |Â
up vote
0
down vote
favorite
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.
bash shell scripting
3
I suggest starting with the Find man page. Then possibly taking a look at How to run find -exec?. Of course it's probably also a good idea if you make a stop Here before writing any shell scripts.
â Jesse_b
Feb 26 at 17:58
How about use find with 'size' parameter? It's very simple: find . -size [+-] <size>.
â Yurij Goncharuk
Feb 26 at 18:10
No need recursion AFAIK, if OP is a beginner, I think he should use basic conditions/algorithm, not advanced tools
â Gilles Quenot
Feb 26 at 18:14
@GillesQuenot: I'm almost positive OPs teacher is trying to point him towardsfind
with this objective.
â Jesse_b
Feb 26 at 18:21
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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.
bash shell scripting
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.
bash shell scripting
asked Feb 26 at 17:57
Arnau MartÃnez
142
142
3
I suggest starting with the Find man page. Then possibly taking a look at How to run find -exec?. Of course it's probably also a good idea if you make a stop Here before writing any shell scripts.
â Jesse_b
Feb 26 at 17:58
How about use find with 'size' parameter? It's very simple: find . -size [+-] <size>.
â Yurij Goncharuk
Feb 26 at 18:10
No need recursion AFAIK, if OP is a beginner, I think he should use basic conditions/algorithm, not advanced tools
â Gilles Quenot
Feb 26 at 18:14
@GillesQuenot: I'm almost positive OPs teacher is trying to point him towardsfind
with this objective.
â Jesse_b
Feb 26 at 18:21
add a comment |Â
3
I suggest starting with the Find man page. Then possibly taking a look at How to run find -exec?. Of course it's probably also a good idea if you make a stop Here before writing any shell scripts.
â Jesse_b
Feb 26 at 17:58
How about use find with 'size' parameter? It's very simple: find . -size [+-] <size>.
â Yurij Goncharuk
Feb 26 at 18:10
No need recursion AFAIK, if OP is a beginner, I think he should use basic conditions/algorithm, not advanced tools
â Gilles Quenot
Feb 26 at 18:14
@GillesQuenot: I'm almost positive OPs teacher is trying to point him towardsfind
with this objective.
â Jesse_b
Feb 26 at 18:21
3
3
I suggest starting with the Find man page. Then possibly taking a look at How to run find -exec?. Of course it's probably also a good idea if you make a stop Here before writing any shell scripts.
â Jesse_b
Feb 26 at 17:58
I suggest starting with the Find man page. Then possibly taking a look at How to run find -exec?. Of course it's probably also a good idea if you make a stop Here before writing any shell scripts.
â Jesse_b
Feb 26 at 17:58
How about use find with 'size' parameter? It's very simple: find . -size [+-] <size>.
â Yurij Goncharuk
Feb 26 at 18:10
How about use find with 'size' parameter? It's very simple: find . -size [+-] <size>.
â Yurij Goncharuk
Feb 26 at 18:10
No need recursion AFAIK, if OP is a beginner, I think he should use basic conditions/algorithm, not advanced tools
â Gilles Quenot
Feb 26 at 18:14
No need recursion AFAIK, if OP is a beginner, I think he should use basic conditions/algorithm, not advanced tools
â Gilles Quenot
Feb 26 at 18:14
@GillesQuenot: I'm almost positive OPs teacher is trying to point him towards
find
with this objective.â Jesse_b
Feb 26 at 18:21
@GillesQuenot: I'm almost positive OPs teacher is trying to point him towards
find
with this objective.â Jesse_b
Feb 26 at 18:21
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
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 find with the -size
switch if you remember your teacher had talked of this tool, ex :
find "$1" -size +100
Check
man find | less +/-size
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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 find with the -size
switch if you remember your teacher had talked of this tool, ex :
find "$1" -size +100
Check
man find | less +/-size
add a comment |Â
up vote
2
down vote
accepted
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 find with the -size
switch if you remember your teacher had talked of this tool, ex :
find "$1" -size +100
Check
man find | less +/-size
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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 find with the -size
switch if you remember your teacher had talked of this tool, ex :
find "$1" -size +100
Check
man find | less +/-size
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 find with the -size
switch if you remember your teacher had talked of this tool, ex :
find "$1" -size +100
Check
man find | less +/-size
edited Feb 26 at 18:48
answered Feb 26 at 18:03
Gilles Quenot
15.3k13448
15.3k13448
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f426758%2fmove-files-by-its-size%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
3
I suggest starting with the Find man page. Then possibly taking a look at How to run find -exec?. Of course it's probably also a good idea if you make a stop Here before writing any shell scripts.
â Jesse_b
Feb 26 at 17:58
How about use find with 'size' parameter? It's very simple: find . -size [+-] <size>.
â Yurij Goncharuk
Feb 26 at 18:10
No need recursion AFAIK, if OP is a beginner, I think he should use basic conditions/algorithm, not advanced tools
â Gilles Quenot
Feb 26 at 18:14
@GillesQuenot: I'm almost positive OPs teacher is trying to point him towards
find
with this objective.â Jesse_b
Feb 26 at 18:21