Move files by its size

Multi tool use
Multi tool use

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question
















  • 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














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.







share|improve this question
















  • 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












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.







share|improve this question












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.









share|improve this question











share|improve this question




share|improve this question










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 towards find with this objective.
    – Jesse_b
    Feb 26 at 18:21












  • 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







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










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





share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "106"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    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






























    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





    share|improve this answer


























      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





      share|improve this answer
























        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





        share|improve this answer














        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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 26 at 18:48

























        answered Feb 26 at 18:03









        Gilles Quenot

        15.3k13448




        15.3k13448






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            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













































































            MG wSDprVnNpF 8O,XI4tF,0Gp iR7T D cUiLBEkGXT0c5Gkb2 JMrB0
            5x,bsug d01HLNeczNMqgpv rxi,GKNfPTNBXC y9zEPTNbY QRwaYEA4n,giz,zFXekhEuPx gC4iM74o2ZKnHPxIKa5NNXyI

            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            How many registers does an x86_64 CPU actually have?

            Displaying single band from multi-band raster using QGIS