Shell script to recursively find and list largest files, ask confirmation to remove them and, if confirmed,...





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I want to write a shell script that recursively finds the largest files with .log extension and lists them out. It has to further ask for the confirmation to remove them. If confirmed, it has to delete that file. I'm new to shell scripting and the closest I got to was this:



#!/bin/bash
cd /
find . -name "*.log" -type f -exec du -sh {} + | sort -rh | head -n 10



This lists out top 10 .log files(largest file first). But, I've no clue how to continue from here. How do I parse this list and ask for the confirmation for removing each file and proceed accordingly?
please help










share|improve this question























  • Why do you say your script needs to be recursive? (This means that your script needs to call itself)

    – Dominique
    Nov 16 '18 at 14:26


















0















I want to write a shell script that recursively finds the largest files with .log extension and lists them out. It has to further ask for the confirmation to remove them. If confirmed, it has to delete that file. I'm new to shell scripting and the closest I got to was this:



#!/bin/bash
cd /
find . -name "*.log" -type f -exec du -sh {} + | sort -rh | head -n 10



This lists out top 10 .log files(largest file first). But, I've no clue how to continue from here. How do I parse this list and ask for the confirmation for removing each file and proceed accordingly?
please help










share|improve this question























  • Why do you say your script needs to be recursive? (This means that your script needs to call itself)

    – Dominique
    Nov 16 '18 at 14:26














0












0








0








I want to write a shell script that recursively finds the largest files with .log extension and lists them out. It has to further ask for the confirmation to remove them. If confirmed, it has to delete that file. I'm new to shell scripting and the closest I got to was this:



#!/bin/bash
cd /
find . -name "*.log" -type f -exec du -sh {} + | sort -rh | head -n 10



This lists out top 10 .log files(largest file first). But, I've no clue how to continue from here. How do I parse this list and ask for the confirmation for removing each file and proceed accordingly?
please help










share|improve this question














I want to write a shell script that recursively finds the largest files with .log extension and lists them out. It has to further ask for the confirmation to remove them. If confirmed, it has to delete that file. I'm new to shell scripting and the closest I got to was this:



#!/bin/bash
cd /
find . -name "*.log" -type f -exec du -sh {} + | sort -rh | head -n 10



This lists out top 10 .log files(largest file first). But, I've no clue how to continue from here. How do I parse this list and ask for the confirmation for removing each file and proceed accordingly?
please help







shell






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 13:42









sdy_159sdy_159

31




31













  • Why do you say your script needs to be recursive? (This means that your script needs to call itself)

    – Dominique
    Nov 16 '18 at 14:26



















  • Why do you say your script needs to be recursive? (This means that your script needs to call itself)

    – Dominique
    Nov 16 '18 at 14:26

















Why do you say your script needs to be recursive? (This means that your script needs to call itself)

– Dominique
Nov 16 '18 at 14:26





Why do you say your script needs to be recursive? (This means that your script needs to call itself)

– Dominique
Nov 16 '18 at 14:26












2 Answers
2






active

oldest

votes


















0














Try



rm -i $(find . -name "*.log" -type f -exec du -sh {} + | sort -rh | head -n 10)


Basically, taking the output of your find command and using it with "rm -i" The "rm -i" forces the rm command to be interactive. It will prompt you if you want to remove a file.



Hope this helps.






share|improve this answer
























  • Thanks. This works.

    – sdy_159
    Nov 16 '18 at 14:41



















0














If you make use of the printf function within find, you can easily list all files and sort them according to their size:



find . -iname '*log' -printf "%s %p" | sort -z -k1rn


We use here the as a line terminator to avoid any issues with possible "funny" filenames. This output can now be piped to a simple while loop that does the queries:



find . -iname '*log' -printf "%s %p" | sort -z -k1rn 
| while read -r -d $'' size file; do
echo "size: $size"
echo "file: $file"
while true; do
read -p "Do you wish to delete this file (y/n) or quit (q)?" ynq
case "$ynq" in
[Yy]* ) rm "$file" ;;
[Nn]* ) continue ;;
[Qq]* ) exit ;;
* ) echo "Please answer yes/no or quit.";;
esac
done
done





share|improve this answer


























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339043%2fshell-script-to-recursively-find-and-list-largest-files-ask-confirmation-to-rem%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Try



    rm -i $(find . -name "*.log" -type f -exec du -sh {} + | sort -rh | head -n 10)


    Basically, taking the output of your find command and using it with "rm -i" The "rm -i" forces the rm command to be interactive. It will prompt you if you want to remove a file.



    Hope this helps.






    share|improve this answer
























    • Thanks. This works.

      – sdy_159
      Nov 16 '18 at 14:41
















    0














    Try



    rm -i $(find . -name "*.log" -type f -exec du -sh {} + | sort -rh | head -n 10)


    Basically, taking the output of your find command and using it with "rm -i" The "rm -i" forces the rm command to be interactive. It will prompt you if you want to remove a file.



    Hope this helps.






    share|improve this answer
























    • Thanks. This works.

      – sdy_159
      Nov 16 '18 at 14:41














    0












    0








    0







    Try



    rm -i $(find . -name "*.log" -type f -exec du -sh {} + | sort -rh | head -n 10)


    Basically, taking the output of your find command and using it with "rm -i" The "rm -i" forces the rm command to be interactive. It will prompt you if you want to remove a file.



    Hope this helps.






    share|improve this answer













    Try



    rm -i $(find . -name "*.log" -type f -exec du -sh {} + | sort -rh | head -n 10)


    Basically, taking the output of your find command and using it with "rm -i" The "rm -i" forces the rm command to be interactive. It will prompt you if you want to remove a file.



    Hope this helps.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 16 '18 at 14:05









    Lewis MLewis M

    49815




    49815













    • Thanks. This works.

      – sdy_159
      Nov 16 '18 at 14:41



















    • Thanks. This works.

      – sdy_159
      Nov 16 '18 at 14:41

















    Thanks. This works.

    – sdy_159
    Nov 16 '18 at 14:41





    Thanks. This works.

    – sdy_159
    Nov 16 '18 at 14:41













    0














    If you make use of the printf function within find, you can easily list all files and sort them according to their size:



    find . -iname '*log' -printf "%s %p" | sort -z -k1rn


    We use here the as a line terminator to avoid any issues with possible "funny" filenames. This output can now be piped to a simple while loop that does the queries:



    find . -iname '*log' -printf "%s %p" | sort -z -k1rn 
    | while read -r -d $'' size file; do
    echo "size: $size"
    echo "file: $file"
    while true; do
    read -p "Do you wish to delete this file (y/n) or quit (q)?" ynq
    case "$ynq" in
    [Yy]* ) rm "$file" ;;
    [Nn]* ) continue ;;
    [Qq]* ) exit ;;
    * ) echo "Please answer yes/no or quit.";;
    esac
    done
    done





    share|improve this answer






























      0














      If you make use of the printf function within find, you can easily list all files and sort them according to their size:



      find . -iname '*log' -printf "%s %p" | sort -z -k1rn


      We use here the as a line terminator to avoid any issues with possible "funny" filenames. This output can now be piped to a simple while loop that does the queries:



      find . -iname '*log' -printf "%s %p" | sort -z -k1rn 
      | while read -r -d $'' size file; do
      echo "size: $size"
      echo "file: $file"
      while true; do
      read -p "Do you wish to delete this file (y/n) or quit (q)?" ynq
      case "$ynq" in
      [Yy]* ) rm "$file" ;;
      [Nn]* ) continue ;;
      [Qq]* ) exit ;;
      * ) echo "Please answer yes/no or quit.";;
      esac
      done
      done





      share|improve this answer




























        0












        0








        0







        If you make use of the printf function within find, you can easily list all files and sort them according to their size:



        find . -iname '*log' -printf "%s %p" | sort -z -k1rn


        We use here the as a line terminator to avoid any issues with possible "funny" filenames. This output can now be piped to a simple while loop that does the queries:



        find . -iname '*log' -printf "%s %p" | sort -z -k1rn 
        | while read -r -d $'' size file; do
        echo "size: $size"
        echo "file: $file"
        while true; do
        read -p "Do you wish to delete this file (y/n) or quit (q)?" ynq
        case "$ynq" in
        [Yy]* ) rm "$file" ;;
        [Nn]* ) continue ;;
        [Qq]* ) exit ;;
        * ) echo "Please answer yes/no or quit.";;
        esac
        done
        done





        share|improve this answer















        If you make use of the printf function within find, you can easily list all files and sort them according to their size:



        find . -iname '*log' -printf "%s %p" | sort -z -k1rn


        We use here the as a line terminator to avoid any issues with possible "funny" filenames. This output can now be piped to a simple while loop that does the queries:



        find . -iname '*log' -printf "%s %p" | sort -z -k1rn 
        | while read -r -d $'' size file; do
        echo "size: $size"
        echo "file: $file"
        while true; do
        read -p "Do you wish to delete this file (y/n) or quit (q)?" ynq
        case "$ynq" in
        [Yy]* ) rm "$file" ;;
        [Nn]* ) continue ;;
        [Qq]* ) exit ;;
        * ) echo "Please answer yes/no or quit.";;
        esac
        done
        done






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 18:05









        marc_s

        584k13011241270




        584k13011241270










        answered Nov 16 '18 at 14:54









        kvantourkvantour

        10.7k41732




        10.7k41732






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53339043%2fshell-script-to-recursively-find-and-list-largest-files-ask-confirmation-to-rem%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine