Delete all directory that begin with a particular string

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











up vote
2
down vote

favorite












What command do I have to use to delete all directories that begin with "graphene-80"



What can I add to the "rm" command as option?







share|improve this question






















  • On the whole system or in a particular directory, or anywhere beneath a particular directory?
    – Kusalananda
    Feb 7 at 14:18











  • in the directory /tmp
    – user1543915
    Feb 7 at 14:22










  • Just empty directories, or the contents too?
    – ilkkachu
    Feb 7 at 15:50














up vote
2
down vote

favorite












What command do I have to use to delete all directories that begin with "graphene-80"



What can I add to the "rm" command as option?







share|improve this question






















  • On the whole system or in a particular directory, or anywhere beneath a particular directory?
    – Kusalananda
    Feb 7 at 14:18











  • in the directory /tmp
    – user1543915
    Feb 7 at 14:22










  • Just empty directories, or the contents too?
    – ilkkachu
    Feb 7 at 15:50












up vote
2
down vote

favorite









up vote
2
down vote

favorite











What command do I have to use to delete all directories that begin with "graphene-80"



What can I add to the "rm" command as option?







share|improve this question














What command do I have to use to delete all directories that begin with "graphene-80"



What can I add to the "rm" command as option?









share|improve this question













share|improve this question




share|improve this question








edited Feb 7 at 14:24









Jeff Schaller

31.3k846105




31.3k846105










asked Feb 7 at 14:10









Thouraya

111




111











  • On the whole system or in a particular directory, or anywhere beneath a particular directory?
    – Kusalananda
    Feb 7 at 14:18











  • in the directory /tmp
    – user1543915
    Feb 7 at 14:22










  • Just empty directories, or the contents too?
    – ilkkachu
    Feb 7 at 15:50
















  • On the whole system or in a particular directory, or anywhere beneath a particular directory?
    – Kusalananda
    Feb 7 at 14:18











  • in the directory /tmp
    – user1543915
    Feb 7 at 14:22










  • Just empty directories, or the contents too?
    – ilkkachu
    Feb 7 at 15:50















On the whole system or in a particular directory, or anywhere beneath a particular directory?
– Kusalananda
Feb 7 at 14:18





On the whole system or in a particular directory, or anywhere beneath a particular directory?
– Kusalananda
Feb 7 at 14:18













in the directory /tmp
– user1543915
Feb 7 at 14:22




in the directory /tmp
– user1543915
Feb 7 at 14:22












Just empty directories, or the contents too?
– ilkkachu
Feb 7 at 15:50




Just empty directories, or the contents too?
– ilkkachu
Feb 7 at 15:50










3 Answers
3






active

oldest

votes

















up vote
2
down vote













Using find command:



find /tmp -type d -name 'graphene-80*' -delete



Arguments used:




  • -type to filter directory only and avoid finding files


  • -name to find file that match the pattern define between quotes


  • -delete to delete the result of the find command


EDIT:
cleaner with -delete like shown in this post:
Find files matching template and remove






share|improve this answer


















  • 2




    Use -depth when deleting directories, or find may complain about "no such directory" when trying to process the directories it has just deleted. You should probably also use -type d to be sure to not delete files.
    – Kusalananda
    Feb 7 at 14:19











  • Indeed, thanks.
    – Kevin Lemaire
    Feb 7 at 14:21






  • 3




    On Linux (as the Q is tagged), -delete will add in -depth for you.
    – Jeff Schaller
    Feb 7 at 14:22










  • find: warning: you have specified the -depth option after a non-option argument -type, but options are not positional (-depth affects tests specified before it as well as those specified after it). Please specify options before other arguments.
    – user1543915
    Feb 7 at 14:37










  • It's corrected.
    – Kevin Lemaire
    Feb 7 at 14:42

















up vote
1
down vote













To delete the directories matching the pattern graphene-80* directly under /tmp, use



rm -rf /tmp/graphene-80*/


Here, the trailing / ensures that only directories whose names match the graphene-80* pattern are deleted, and not files etc.



To find the matching directories elsewhere under /tmp and delete them wherever they may be (or to handle the case where there are too many matching names resulting in a too long command), use



find /tmp -depth -type d -name 'graphene-80*' -prune -exec rm -rf ';'


To additionally see the names of the directories as they are deleted, insert -print before -exec. Inserting -print after -exec will print the names of directories succesfully deleted.






share|improve this answer






















  • find -delete will only delete directories if they are empty. You'd probably want -name 'graphene-80*' -prune -exec rm -rf +, or with -delete, use some -path approach to also delete the files inside those directories (note that -delete implies -depth)
    – Stéphane Chazelas
    Feb 8 at 11:03










  • You'd want to use -prune as well to not descend into the directories you're going to delete anyway.
    – Stéphane Chazelas
    Feb 8 at 11:12











  • @StéphaneChazelas Thanks, I was mislead by Jeff's comment to another answer and assumed it worked the way I thought it would without testing.
    – Kusalananda
    Feb 8 at 11:12










  • You should choose between + and ;. Note that with +, -exec always returns true since the execution of the command is postponed (see also rm -v with some implementations to print deleted files)
    – Stéphane Chazelas
    Feb 8 at 11:14











  • @StéphaneChazelas Sorted. I have too much in my head today. I appreciate your input as always.
    – Kusalananda
    Feb 8 at 11:16

















up vote
0
down vote













To remove just empty directories with names matching graphene-80*, either



find /tmp -type d -name "graphene-80*" -exec rmdir +


or



find /tmp -type d -name "graphene-80*" -delete


(rmdir and GNU find should give you errors for the nonempty ones.)



To remove the directories with their contents:



find /tmp -type d -name "graphene-80*" -exec rm -r ; -prune


(With -exec rm + you may get errors from rm if there are nested matching directories; and without -prune, from find since it tries to descend to the just-removed directories.)






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%2f422548%2fdelete-all-directory-that-begin-with-a-particular-string%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
    2
    down vote













    Using find command:



    find /tmp -type d -name 'graphene-80*' -delete



    Arguments used:




    • -type to filter directory only and avoid finding files


    • -name to find file that match the pattern define between quotes


    • -delete to delete the result of the find command


    EDIT:
    cleaner with -delete like shown in this post:
    Find files matching template and remove






    share|improve this answer


















    • 2




      Use -depth when deleting directories, or find may complain about "no such directory" when trying to process the directories it has just deleted. You should probably also use -type d to be sure to not delete files.
      – Kusalananda
      Feb 7 at 14:19











    • Indeed, thanks.
      – Kevin Lemaire
      Feb 7 at 14:21






    • 3




      On Linux (as the Q is tagged), -delete will add in -depth for you.
      – Jeff Schaller
      Feb 7 at 14:22










    • find: warning: you have specified the -depth option after a non-option argument -type, but options are not positional (-depth affects tests specified before it as well as those specified after it). Please specify options before other arguments.
      – user1543915
      Feb 7 at 14:37










    • It's corrected.
      – Kevin Lemaire
      Feb 7 at 14:42














    up vote
    2
    down vote













    Using find command:



    find /tmp -type d -name 'graphene-80*' -delete



    Arguments used:




    • -type to filter directory only and avoid finding files


    • -name to find file that match the pattern define between quotes


    • -delete to delete the result of the find command


    EDIT:
    cleaner with -delete like shown in this post:
    Find files matching template and remove






    share|improve this answer


















    • 2




      Use -depth when deleting directories, or find may complain about "no such directory" when trying to process the directories it has just deleted. You should probably also use -type d to be sure to not delete files.
      – Kusalananda
      Feb 7 at 14:19











    • Indeed, thanks.
      – Kevin Lemaire
      Feb 7 at 14:21






    • 3




      On Linux (as the Q is tagged), -delete will add in -depth for you.
      – Jeff Schaller
      Feb 7 at 14:22










    • find: warning: you have specified the -depth option after a non-option argument -type, but options are not positional (-depth affects tests specified before it as well as those specified after it). Please specify options before other arguments.
      – user1543915
      Feb 7 at 14:37










    • It's corrected.
      – Kevin Lemaire
      Feb 7 at 14:42












    up vote
    2
    down vote










    up vote
    2
    down vote









    Using find command:



    find /tmp -type d -name 'graphene-80*' -delete



    Arguments used:




    • -type to filter directory only and avoid finding files


    • -name to find file that match the pattern define between quotes


    • -delete to delete the result of the find command


    EDIT:
    cleaner with -delete like shown in this post:
    Find files matching template and remove






    share|improve this answer














    Using find command:



    find /tmp -type d -name 'graphene-80*' -delete



    Arguments used:




    • -type to filter directory only and avoid finding files


    • -name to find file that match the pattern define between quotes


    • -delete to delete the result of the find command


    EDIT:
    cleaner with -delete like shown in this post:
    Find files matching template and remove







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Feb 7 at 14:38

























    answered Feb 7 at 14:17









    Kevin Lemaire

    1,037421




    1,037421







    • 2




      Use -depth when deleting directories, or find may complain about "no such directory" when trying to process the directories it has just deleted. You should probably also use -type d to be sure to not delete files.
      – Kusalananda
      Feb 7 at 14:19











    • Indeed, thanks.
      – Kevin Lemaire
      Feb 7 at 14:21






    • 3




      On Linux (as the Q is tagged), -delete will add in -depth for you.
      – Jeff Schaller
      Feb 7 at 14:22










    • find: warning: you have specified the -depth option after a non-option argument -type, but options are not positional (-depth affects tests specified before it as well as those specified after it). Please specify options before other arguments.
      – user1543915
      Feb 7 at 14:37










    • It's corrected.
      – Kevin Lemaire
      Feb 7 at 14:42












    • 2




      Use -depth when deleting directories, or find may complain about "no such directory" when trying to process the directories it has just deleted. You should probably also use -type d to be sure to not delete files.
      – Kusalananda
      Feb 7 at 14:19











    • Indeed, thanks.
      – Kevin Lemaire
      Feb 7 at 14:21






    • 3




      On Linux (as the Q is tagged), -delete will add in -depth for you.
      – Jeff Schaller
      Feb 7 at 14:22










    • find: warning: you have specified the -depth option after a non-option argument -type, but options are not positional (-depth affects tests specified before it as well as those specified after it). Please specify options before other arguments.
      – user1543915
      Feb 7 at 14:37










    • It's corrected.
      – Kevin Lemaire
      Feb 7 at 14:42







    2




    2




    Use -depth when deleting directories, or find may complain about "no such directory" when trying to process the directories it has just deleted. You should probably also use -type d to be sure to not delete files.
    – Kusalananda
    Feb 7 at 14:19





    Use -depth when deleting directories, or find may complain about "no such directory" when trying to process the directories it has just deleted. You should probably also use -type d to be sure to not delete files.
    – Kusalananda
    Feb 7 at 14:19













    Indeed, thanks.
    – Kevin Lemaire
    Feb 7 at 14:21




    Indeed, thanks.
    – Kevin Lemaire
    Feb 7 at 14:21




    3




    3




    On Linux (as the Q is tagged), -delete will add in -depth for you.
    – Jeff Schaller
    Feb 7 at 14:22




    On Linux (as the Q is tagged), -delete will add in -depth for you.
    – Jeff Schaller
    Feb 7 at 14:22












    find: warning: you have specified the -depth option after a non-option argument -type, but options are not positional (-depth affects tests specified before it as well as those specified after it). Please specify options before other arguments.
    – user1543915
    Feb 7 at 14:37




    find: warning: you have specified the -depth option after a non-option argument -type, but options are not positional (-depth affects tests specified before it as well as those specified after it). Please specify options before other arguments.
    – user1543915
    Feb 7 at 14:37












    It's corrected.
    – Kevin Lemaire
    Feb 7 at 14:42




    It's corrected.
    – Kevin Lemaire
    Feb 7 at 14:42












    up vote
    1
    down vote













    To delete the directories matching the pattern graphene-80* directly under /tmp, use



    rm -rf /tmp/graphene-80*/


    Here, the trailing / ensures that only directories whose names match the graphene-80* pattern are deleted, and not files etc.



    To find the matching directories elsewhere under /tmp and delete them wherever they may be (or to handle the case where there are too many matching names resulting in a too long command), use



    find /tmp -depth -type d -name 'graphene-80*' -prune -exec rm -rf ';'


    To additionally see the names of the directories as they are deleted, insert -print before -exec. Inserting -print after -exec will print the names of directories succesfully deleted.






    share|improve this answer






















    • find -delete will only delete directories if they are empty. You'd probably want -name 'graphene-80*' -prune -exec rm -rf +, or with -delete, use some -path approach to also delete the files inside those directories (note that -delete implies -depth)
      – Stéphane Chazelas
      Feb 8 at 11:03










    • You'd want to use -prune as well to not descend into the directories you're going to delete anyway.
      – Stéphane Chazelas
      Feb 8 at 11:12











    • @StéphaneChazelas Thanks, I was mislead by Jeff's comment to another answer and assumed it worked the way I thought it would without testing.
      – Kusalananda
      Feb 8 at 11:12










    • You should choose between + and ;. Note that with +, -exec always returns true since the execution of the command is postponed (see also rm -v with some implementations to print deleted files)
      – Stéphane Chazelas
      Feb 8 at 11:14











    • @StéphaneChazelas Sorted. I have too much in my head today. I appreciate your input as always.
      – Kusalananda
      Feb 8 at 11:16














    up vote
    1
    down vote













    To delete the directories matching the pattern graphene-80* directly under /tmp, use



    rm -rf /tmp/graphene-80*/


    Here, the trailing / ensures that only directories whose names match the graphene-80* pattern are deleted, and not files etc.



    To find the matching directories elsewhere under /tmp and delete them wherever they may be (or to handle the case where there are too many matching names resulting in a too long command), use



    find /tmp -depth -type d -name 'graphene-80*' -prune -exec rm -rf ';'


    To additionally see the names of the directories as they are deleted, insert -print before -exec. Inserting -print after -exec will print the names of directories succesfully deleted.






    share|improve this answer






















    • find -delete will only delete directories if they are empty. You'd probably want -name 'graphene-80*' -prune -exec rm -rf +, or with -delete, use some -path approach to also delete the files inside those directories (note that -delete implies -depth)
      – Stéphane Chazelas
      Feb 8 at 11:03










    • You'd want to use -prune as well to not descend into the directories you're going to delete anyway.
      – Stéphane Chazelas
      Feb 8 at 11:12











    • @StéphaneChazelas Thanks, I was mislead by Jeff's comment to another answer and assumed it worked the way I thought it would without testing.
      – Kusalananda
      Feb 8 at 11:12










    • You should choose between + and ;. Note that with +, -exec always returns true since the execution of the command is postponed (see also rm -v with some implementations to print deleted files)
      – Stéphane Chazelas
      Feb 8 at 11:14











    • @StéphaneChazelas Sorted. I have too much in my head today. I appreciate your input as always.
      – Kusalananda
      Feb 8 at 11:16












    up vote
    1
    down vote










    up vote
    1
    down vote









    To delete the directories matching the pattern graphene-80* directly under /tmp, use



    rm -rf /tmp/graphene-80*/


    Here, the trailing / ensures that only directories whose names match the graphene-80* pattern are deleted, and not files etc.



    To find the matching directories elsewhere under /tmp and delete them wherever they may be (or to handle the case where there are too many matching names resulting in a too long command), use



    find /tmp -depth -type d -name 'graphene-80*' -prune -exec rm -rf ';'


    To additionally see the names of the directories as they are deleted, insert -print before -exec. Inserting -print after -exec will print the names of directories succesfully deleted.






    share|improve this answer














    To delete the directories matching the pattern graphene-80* directly under /tmp, use



    rm -rf /tmp/graphene-80*/


    Here, the trailing / ensures that only directories whose names match the graphene-80* pattern are deleted, and not files etc.



    To find the matching directories elsewhere under /tmp and delete them wherever they may be (or to handle the case where there are too many matching names resulting in a too long command), use



    find /tmp -depth -type d -name 'graphene-80*' -prune -exec rm -rf ';'


    To additionally see the names of the directories as they are deleted, insert -print before -exec. Inserting -print after -exec will print the names of directories succesfully deleted.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Feb 8 at 11:15

























    answered Feb 7 at 14:38









    Kusalananda

    103k13202318




    103k13202318











    • find -delete will only delete directories if they are empty. You'd probably want -name 'graphene-80*' -prune -exec rm -rf +, or with -delete, use some -path approach to also delete the files inside those directories (note that -delete implies -depth)
      – Stéphane Chazelas
      Feb 8 at 11:03










    • You'd want to use -prune as well to not descend into the directories you're going to delete anyway.
      – Stéphane Chazelas
      Feb 8 at 11:12











    • @StéphaneChazelas Thanks, I was mislead by Jeff's comment to another answer and assumed it worked the way I thought it would without testing.
      – Kusalananda
      Feb 8 at 11:12










    • You should choose between + and ;. Note that with +, -exec always returns true since the execution of the command is postponed (see also rm -v with some implementations to print deleted files)
      – Stéphane Chazelas
      Feb 8 at 11:14











    • @StéphaneChazelas Sorted. I have too much in my head today. I appreciate your input as always.
      – Kusalananda
      Feb 8 at 11:16
















    • find -delete will only delete directories if they are empty. You'd probably want -name 'graphene-80*' -prune -exec rm -rf +, or with -delete, use some -path approach to also delete the files inside those directories (note that -delete implies -depth)
      – Stéphane Chazelas
      Feb 8 at 11:03










    • You'd want to use -prune as well to not descend into the directories you're going to delete anyway.
      – Stéphane Chazelas
      Feb 8 at 11:12











    • @StéphaneChazelas Thanks, I was mislead by Jeff's comment to another answer and assumed it worked the way I thought it would without testing.
      – Kusalananda
      Feb 8 at 11:12










    • You should choose between + and ;. Note that with +, -exec always returns true since the execution of the command is postponed (see also rm -v with some implementations to print deleted files)
      – Stéphane Chazelas
      Feb 8 at 11:14











    • @StéphaneChazelas Sorted. I have too much in my head today. I appreciate your input as always.
      – Kusalananda
      Feb 8 at 11:16















    find -delete will only delete directories if they are empty. You'd probably want -name 'graphene-80*' -prune -exec rm -rf +, or with -delete, use some -path approach to also delete the files inside those directories (note that -delete implies -depth)
    – Stéphane Chazelas
    Feb 8 at 11:03




    find -delete will only delete directories if they are empty. You'd probably want -name 'graphene-80*' -prune -exec rm -rf +, or with -delete, use some -path approach to also delete the files inside those directories (note that -delete implies -depth)
    – Stéphane Chazelas
    Feb 8 at 11:03












    You'd want to use -prune as well to not descend into the directories you're going to delete anyway.
    – Stéphane Chazelas
    Feb 8 at 11:12





    You'd want to use -prune as well to not descend into the directories you're going to delete anyway.
    – Stéphane Chazelas
    Feb 8 at 11:12













    @StéphaneChazelas Thanks, I was mislead by Jeff's comment to another answer and assumed it worked the way I thought it would without testing.
    – Kusalananda
    Feb 8 at 11:12




    @StéphaneChazelas Thanks, I was mislead by Jeff's comment to another answer and assumed it worked the way I thought it would without testing.
    – Kusalananda
    Feb 8 at 11:12












    You should choose between + and ;. Note that with +, -exec always returns true since the execution of the command is postponed (see also rm -v with some implementations to print deleted files)
    – Stéphane Chazelas
    Feb 8 at 11:14





    You should choose between + and ;. Note that with +, -exec always returns true since the execution of the command is postponed (see also rm -v with some implementations to print deleted files)
    – Stéphane Chazelas
    Feb 8 at 11:14













    @StéphaneChazelas Sorted. I have too much in my head today. I appreciate your input as always.
    – Kusalananda
    Feb 8 at 11:16




    @StéphaneChazelas Sorted. I have too much in my head today. I appreciate your input as always.
    – Kusalananda
    Feb 8 at 11:16










    up vote
    0
    down vote













    To remove just empty directories with names matching graphene-80*, either



    find /tmp -type d -name "graphene-80*" -exec rmdir +


    or



    find /tmp -type d -name "graphene-80*" -delete


    (rmdir and GNU find should give you errors for the nonempty ones.)



    To remove the directories with their contents:



    find /tmp -type d -name "graphene-80*" -exec rm -r ; -prune


    (With -exec rm + you may get errors from rm if there are nested matching directories; and without -prune, from find since it tries to descend to the just-removed directories.)






    share|improve this answer


























      up vote
      0
      down vote













      To remove just empty directories with names matching graphene-80*, either



      find /tmp -type d -name "graphene-80*" -exec rmdir +


      or



      find /tmp -type d -name "graphene-80*" -delete


      (rmdir and GNU find should give you errors for the nonempty ones.)



      To remove the directories with their contents:



      find /tmp -type d -name "graphene-80*" -exec rm -r ; -prune


      (With -exec rm + you may get errors from rm if there are nested matching directories; and without -prune, from find since it tries to descend to the just-removed directories.)






      share|improve this answer
























        up vote
        0
        down vote










        up vote
        0
        down vote









        To remove just empty directories with names matching graphene-80*, either



        find /tmp -type d -name "graphene-80*" -exec rmdir +


        or



        find /tmp -type d -name "graphene-80*" -delete


        (rmdir and GNU find should give you errors for the nonempty ones.)



        To remove the directories with their contents:



        find /tmp -type d -name "graphene-80*" -exec rm -r ; -prune


        (With -exec rm + you may get errors from rm if there are nested matching directories; and without -prune, from find since it tries to descend to the just-removed directories.)






        share|improve this answer














        To remove just empty directories with names matching graphene-80*, either



        find /tmp -type d -name "graphene-80*" -exec rmdir +


        or



        find /tmp -type d -name "graphene-80*" -delete


        (rmdir and GNU find should give you errors for the nonempty ones.)



        To remove the directories with their contents:



        find /tmp -type d -name "graphene-80*" -exec rm -r ; -prune


        (With -exec rm + you may get errors from rm if there are nested matching directories; and without -prune, from find since it tries to descend to the just-removed directories.)







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Feb 7 at 16:23

























        answered Feb 7 at 15:59









        ilkkachu

        49.6k673137




        49.6k673137






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f422548%2fdelete-all-directory-that-begin-with-a-particular-string%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?