Remove files + files from subdirectories in Directory

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
0
down vote

favorite












I would like to remove all files from a directory /data which includes 8 other sub directories.



Is there a command which will recursively clear all subdirectories but not remove the actual folders?










share|improve this question







New contributor




nm97 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Related: unix.stackexchange.com/q/182033/117549
    – Jeff Schaller
    6 hours ago














up vote
0
down vote

favorite












I would like to remove all files from a directory /data which includes 8 other sub directories.



Is there a command which will recursively clear all subdirectories but not remove the actual folders?










share|improve this question







New contributor




nm97 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



















  • Related: unix.stackexchange.com/q/182033/117549
    – Jeff Schaller
    6 hours ago












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I would like to remove all files from a directory /data which includes 8 other sub directories.



Is there a command which will recursively clear all subdirectories but not remove the actual folders?










share|improve this question







New contributor




nm97 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I would like to remove all files from a directory /data which includes 8 other sub directories.



Is there a command which will recursively clear all subdirectories but not remove the actual folders?







rm






share|improve this question







New contributor




nm97 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




nm97 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




nm97 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 6 hours ago









nm97

11




11




New contributor




nm97 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





nm97 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






nm97 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











  • Related: unix.stackexchange.com/q/182033/117549
    – Jeff Schaller
    6 hours ago
















  • Related: unix.stackexchange.com/q/182033/117549
    – Jeff Schaller
    6 hours ago















Related: unix.stackexchange.com/q/182033/117549
– Jeff Schaller
6 hours ago




Related: unix.stackexchange.com/q/182033/117549
– Jeff Schaller
6 hours ago










3 Answers
3






active

oldest

votes

















up vote
3
down vote













The following would delete any non-directory files, like regular files, symbolic links, named pipes, sockets etc., in or under the /data directory:



find /data ! -type d -delete


For implementations of find that does not have the non-standard predicate -delete, use -exec rm -f + in its place:



find /data ! -type d -exec rm -f +


This would find all non-directory files in or under /data and would execute rm -f on as large batches of these as possible.






share|improve this answer





























    up vote
    0
    down vote













    Use find command



    find /data -type f -exec rm -rf ; 


    will delete only files due to the type selection type f for files.






    share|improve this answer
















    • 1




      No need for -r if you're removing files...
      – Jeff Schaller
      6 hours ago

















    up vote
    0
    down vote













    You can use the find command for this.



    To create a test case to reproduce your description, let me do this:
    1. cd /tmp
    2. mkdir -p testing/a,b,c
    3. cd testing/
    4. touch a,b,c/1,2,3



    To verify there are multiple directories each containing multiple files:



    $ find -type f
    ./c/3
    ./c/2
    ./c/1
    ./b/3
    ./b/2
    ./b/1
    ./a/3
    ./a/2
    ./a/1


    You can now use find again to delete whatever it finds:



    find -type f -delete


    If you now run find -type f again it will not return any results, because the files are gone, but you can see that the directories still exist:



    $ ls
    a b c


    The find command is very powerful. You can discover more about it using man find.






    share|improve this answer
















    • 1




      The question isn't yet tagged Linux, so I'll just note that -delete is a gnu extension to find.
      – Jeff Schaller
      6 hours ago










    • @JeffSchaller Ah, noted. Thanks for that, it wasn't on my radar that -delete is a GNUism :)
      – cryptarch
      6 hours ago






    • 1




      @JeffSchaller, -delete is from FreeBSD (1996). Only added to GNU find in 4.2.3 (2004) (and to NetBSD find in 2007)
      – Stéphane Chazelas
      5 hours ago






    • 1




      @StéphaneChazelas -delete was added in January 2017 to OpenBSD find, if you want to be complete...
      – Kusalananda
      5 hours ago











    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: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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
    );



    );






    nm97 is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f481152%2fremove-files-files-from-subdirectories-in-directory%23new-answer', 'question_page');

    );

    Post as a guest






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote













    The following would delete any non-directory files, like regular files, symbolic links, named pipes, sockets etc., in or under the /data directory:



    find /data ! -type d -delete


    For implementations of find that does not have the non-standard predicate -delete, use -exec rm -f + in its place:



    find /data ! -type d -exec rm -f +


    This would find all non-directory files in or under /data and would execute rm -f on as large batches of these as possible.






    share|improve this answer


























      up vote
      3
      down vote













      The following would delete any non-directory files, like regular files, symbolic links, named pipes, sockets etc., in or under the /data directory:



      find /data ! -type d -delete


      For implementations of find that does not have the non-standard predicate -delete, use -exec rm -f + in its place:



      find /data ! -type d -exec rm -f +


      This would find all non-directory files in or under /data and would execute rm -f on as large batches of these as possible.






      share|improve this answer
























        up vote
        3
        down vote










        up vote
        3
        down vote









        The following would delete any non-directory files, like regular files, symbolic links, named pipes, sockets etc., in or under the /data directory:



        find /data ! -type d -delete


        For implementations of find that does not have the non-standard predicate -delete, use -exec rm -f + in its place:



        find /data ! -type d -exec rm -f +


        This would find all non-directory files in or under /data and would execute rm -f on as large batches of these as possible.






        share|improve this answer














        The following would delete any non-directory files, like regular files, symbolic links, named pipes, sockets etc., in or under the /data directory:



        find /data ! -type d -delete


        For implementations of find that does not have the non-standard predicate -delete, use -exec rm -f + in its place:



        find /data ! -type d -exec rm -f +


        This would find all non-directory files in or under /data and would execute rm -f on as large batches of these as possible.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 6 hours ago

























        answered 6 hours ago









        Kusalananda

        114k15218349




        114k15218349






















            up vote
            0
            down vote













            Use find command



            find /data -type f -exec rm -rf ; 


            will delete only files due to the type selection type f for files.






            share|improve this answer
















            • 1




              No need for -r if you're removing files...
              – Jeff Schaller
              6 hours ago














            up vote
            0
            down vote













            Use find command



            find /data -type f -exec rm -rf ; 


            will delete only files due to the type selection type f for files.






            share|improve this answer
















            • 1




              No need for -r if you're removing files...
              – Jeff Schaller
              6 hours ago












            up vote
            0
            down vote










            up vote
            0
            down vote









            Use find command



            find /data -type f -exec rm -rf ; 


            will delete only files due to the type selection type f for files.






            share|improve this answer












            Use find command



            find /data -type f -exec rm -rf ; 


            will delete only files due to the type selection type f for files.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 6 hours ago









            francois P

            909114




            909114







            • 1




              No need for -r if you're removing files...
              – Jeff Schaller
              6 hours ago












            • 1




              No need for -r if you're removing files...
              – Jeff Schaller
              6 hours ago







            1




            1




            No need for -r if you're removing files...
            – Jeff Schaller
            6 hours ago




            No need for -r if you're removing files...
            – Jeff Schaller
            6 hours ago










            up vote
            0
            down vote













            You can use the find command for this.



            To create a test case to reproduce your description, let me do this:
            1. cd /tmp
            2. mkdir -p testing/a,b,c
            3. cd testing/
            4. touch a,b,c/1,2,3



            To verify there are multiple directories each containing multiple files:



            $ find -type f
            ./c/3
            ./c/2
            ./c/1
            ./b/3
            ./b/2
            ./b/1
            ./a/3
            ./a/2
            ./a/1


            You can now use find again to delete whatever it finds:



            find -type f -delete


            If you now run find -type f again it will not return any results, because the files are gone, but you can see that the directories still exist:



            $ ls
            a b c


            The find command is very powerful. You can discover more about it using man find.






            share|improve this answer
















            • 1




              The question isn't yet tagged Linux, so I'll just note that -delete is a gnu extension to find.
              – Jeff Schaller
              6 hours ago










            • @JeffSchaller Ah, noted. Thanks for that, it wasn't on my radar that -delete is a GNUism :)
              – cryptarch
              6 hours ago






            • 1




              @JeffSchaller, -delete is from FreeBSD (1996). Only added to GNU find in 4.2.3 (2004) (and to NetBSD find in 2007)
              – Stéphane Chazelas
              5 hours ago






            • 1




              @StéphaneChazelas -delete was added in January 2017 to OpenBSD find, if you want to be complete...
              – Kusalananda
              5 hours ago















            up vote
            0
            down vote













            You can use the find command for this.



            To create a test case to reproduce your description, let me do this:
            1. cd /tmp
            2. mkdir -p testing/a,b,c
            3. cd testing/
            4. touch a,b,c/1,2,3



            To verify there are multiple directories each containing multiple files:



            $ find -type f
            ./c/3
            ./c/2
            ./c/1
            ./b/3
            ./b/2
            ./b/1
            ./a/3
            ./a/2
            ./a/1


            You can now use find again to delete whatever it finds:



            find -type f -delete


            If you now run find -type f again it will not return any results, because the files are gone, but you can see that the directories still exist:



            $ ls
            a b c


            The find command is very powerful. You can discover more about it using man find.






            share|improve this answer
















            • 1




              The question isn't yet tagged Linux, so I'll just note that -delete is a gnu extension to find.
              – Jeff Schaller
              6 hours ago










            • @JeffSchaller Ah, noted. Thanks for that, it wasn't on my radar that -delete is a GNUism :)
              – cryptarch
              6 hours ago






            • 1




              @JeffSchaller, -delete is from FreeBSD (1996). Only added to GNU find in 4.2.3 (2004) (and to NetBSD find in 2007)
              – Stéphane Chazelas
              5 hours ago






            • 1




              @StéphaneChazelas -delete was added in January 2017 to OpenBSD find, if you want to be complete...
              – Kusalananda
              5 hours ago













            up vote
            0
            down vote










            up vote
            0
            down vote









            You can use the find command for this.



            To create a test case to reproduce your description, let me do this:
            1. cd /tmp
            2. mkdir -p testing/a,b,c
            3. cd testing/
            4. touch a,b,c/1,2,3



            To verify there are multiple directories each containing multiple files:



            $ find -type f
            ./c/3
            ./c/2
            ./c/1
            ./b/3
            ./b/2
            ./b/1
            ./a/3
            ./a/2
            ./a/1


            You can now use find again to delete whatever it finds:



            find -type f -delete


            If you now run find -type f again it will not return any results, because the files are gone, but you can see that the directories still exist:



            $ ls
            a b c


            The find command is very powerful. You can discover more about it using man find.






            share|improve this answer












            You can use the find command for this.



            To create a test case to reproduce your description, let me do this:
            1. cd /tmp
            2. mkdir -p testing/a,b,c
            3. cd testing/
            4. touch a,b,c/1,2,3



            To verify there are multiple directories each containing multiple files:



            $ find -type f
            ./c/3
            ./c/2
            ./c/1
            ./b/3
            ./b/2
            ./b/1
            ./a/3
            ./a/2
            ./a/1


            You can now use find again to delete whatever it finds:



            find -type f -delete


            If you now run find -type f again it will not return any results, because the files are gone, but you can see that the directories still exist:



            $ ls
            a b c


            The find command is very powerful. You can discover more about it using man find.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 6 hours ago









            cryptarch

            2514




            2514







            • 1




              The question isn't yet tagged Linux, so I'll just note that -delete is a gnu extension to find.
              – Jeff Schaller
              6 hours ago










            • @JeffSchaller Ah, noted. Thanks for that, it wasn't on my radar that -delete is a GNUism :)
              – cryptarch
              6 hours ago






            • 1




              @JeffSchaller, -delete is from FreeBSD (1996). Only added to GNU find in 4.2.3 (2004) (and to NetBSD find in 2007)
              – Stéphane Chazelas
              5 hours ago






            • 1




              @StéphaneChazelas -delete was added in January 2017 to OpenBSD find, if you want to be complete...
              – Kusalananda
              5 hours ago













            • 1




              The question isn't yet tagged Linux, so I'll just note that -delete is a gnu extension to find.
              – Jeff Schaller
              6 hours ago










            • @JeffSchaller Ah, noted. Thanks for that, it wasn't on my radar that -delete is a GNUism :)
              – cryptarch
              6 hours ago






            • 1




              @JeffSchaller, -delete is from FreeBSD (1996). Only added to GNU find in 4.2.3 (2004) (and to NetBSD find in 2007)
              – Stéphane Chazelas
              5 hours ago






            • 1




              @StéphaneChazelas -delete was added in January 2017 to OpenBSD find, if you want to be complete...
              – Kusalananda
              5 hours ago








            1




            1




            The question isn't yet tagged Linux, so I'll just note that -delete is a gnu extension to find.
            – Jeff Schaller
            6 hours ago




            The question isn't yet tagged Linux, so I'll just note that -delete is a gnu extension to find.
            – Jeff Schaller
            6 hours ago












            @JeffSchaller Ah, noted. Thanks for that, it wasn't on my radar that -delete is a GNUism :)
            – cryptarch
            6 hours ago




            @JeffSchaller Ah, noted. Thanks for that, it wasn't on my radar that -delete is a GNUism :)
            – cryptarch
            6 hours ago




            1




            1




            @JeffSchaller, -delete is from FreeBSD (1996). Only added to GNU find in 4.2.3 (2004) (and to NetBSD find in 2007)
            – Stéphane Chazelas
            5 hours ago




            @JeffSchaller, -delete is from FreeBSD (1996). Only added to GNU find in 4.2.3 (2004) (and to NetBSD find in 2007)
            – Stéphane Chazelas
            5 hours ago




            1




            1




            @StéphaneChazelas -delete was added in January 2017 to OpenBSD find, if you want to be complete...
            – Kusalananda
            5 hours ago





            @StéphaneChazelas -delete was added in January 2017 to OpenBSD find, if you want to be complete...
            – Kusalananda
            5 hours ago











            nm97 is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            nm97 is a new contributor. Be nice, and check out our Code of Conduct.












            nm97 is a new contributor. Be nice, and check out our Code of Conduct.











            nm97 is a new contributor. Be nice, and check out our Code of Conduct.













             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f481152%2fremove-files-files-from-subdirectories-in-directory%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

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

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?