Which file first get deleted first when deleting via rm -f with a glob pattern?

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











up vote
0
down vote

favorite












I've a folder with thousands of files with this pattern:
YYYYMMDD_HH24MISS_4DIGITSEQUENCE.



Example: 20180626_210123_0001.



Now for example while I'm deleting via
rm -f *20180626_1* command its working fine.



My question is:
1. Which file is being deleted first?
2. Is it being picked randomnly?
3. If it maintains any sequence while choosing, then which one and how it decides?







share|improve this question

















  • 1




    See How does a shell (bash, for example) expand wildcard patterns? and Does the Bash star * wildcard always produce an (ascending) sorted list?
    – steeldriver
    Jun 26 at 15:27














up vote
0
down vote

favorite












I've a folder with thousands of files with this pattern:
YYYYMMDD_HH24MISS_4DIGITSEQUENCE.



Example: 20180626_210123_0001.



Now for example while I'm deleting via
rm -f *20180626_1* command its working fine.



My question is:
1. Which file is being deleted first?
2. Is it being picked randomnly?
3. If it maintains any sequence while choosing, then which one and how it decides?







share|improve this question

















  • 1




    See How does a shell (bash, for example) expand wildcard patterns? and Does the Bash star * wildcard always produce an (ascending) sorted list?
    – steeldriver
    Jun 26 at 15:27












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I've a folder with thousands of files with this pattern:
YYYYMMDD_HH24MISS_4DIGITSEQUENCE.



Example: 20180626_210123_0001.



Now for example while I'm deleting via
rm -f *20180626_1* command its working fine.



My question is:
1. Which file is being deleted first?
2. Is it being picked randomnly?
3. If it maintains any sequence while choosing, then which one and how it decides?







share|improve this question













I've a folder with thousands of files with this pattern:
YYYYMMDD_HH24MISS_4DIGITSEQUENCE.



Example: 20180626_210123_0001.



Now for example while I'm deleting via
rm -f *20180626_1* command its working fine.



My question is:
1. Which file is being deleted first?
2. Is it being picked randomnly?
3. If it maintains any sequence while choosing, then which one and how it decides?









share|improve this question












share|improve this question




share|improve this question








edited Jun 26 at 15:21









ilkkachu

47.3k668130




47.3k668130









asked Jun 26 at 15:17









Leon

1033




1033







  • 1




    See How does a shell (bash, for example) expand wildcard patterns? and Does the Bash star * wildcard always produce an (ascending) sorted list?
    – steeldriver
    Jun 26 at 15:27












  • 1




    See How does a shell (bash, for example) expand wildcard patterns? and Does the Bash star * wildcard always produce an (ascending) sorted list?
    – steeldriver
    Jun 26 at 15:27







1




1




See How does a shell (bash, for example) expand wildcard patterns? and Does the Bash star * wildcard always produce an (ascending) sorted list?
– steeldriver
Jun 26 at 15:27




See How does a shell (bash, for example) expand wildcard patterns? and Does the Bash star * wildcard always produce an (ascending) sorted list?
– steeldriver
Jun 26 at 15:27










2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










Globs are expanded by the shell in lexicographic order (in the current locale), and the rm implementation is likely to remove the files it gets as arguments in the same order it got them. So in your case the files would be removed starting with the oldest.



Sorting the glob results is required by POSIX. On a quick test, at least GNU rm removes files given on the command line in the order listed, and doesn't sort files found during recursive operation.






share|improve this answer























  • I looked at the GNU rm implementation and traced my way down into the fts calls from rm.c. There is one call to fts_sort but it's conditional and I'm not 100% sure what it actually does.
    – Kusalananda
    Jun 26 at 15:33











  • @Kusalananda, I can't really understand why rm would want to sort anything...
    – ilkkachu
    Jun 26 at 15:50










  • Me neither, but there it is.
    – Kusalananda
    Jun 26 at 15:54

















up vote
1
down vote













The shell expands the expression before it executes rm, and that expansion is not specific to the command, so if you do:



echo *20180626_1*


Then the first thing that echos will be the first thing that the shell passes to rm. The order isn't random, it's alphabetical. From the bash man page:




After word splitting, unless the -f option has been set, bash scans each word for the characters *, ?, and [. If one of these characters appears, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of file names matching the pattern.







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%2f452035%2fwhich-file-first-get-deleted-first-when-deleting-via-rm-f-with-a-glob-pattern%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Globs are expanded by the shell in lexicographic order (in the current locale), and the rm implementation is likely to remove the files it gets as arguments in the same order it got them. So in your case the files would be removed starting with the oldest.



    Sorting the glob results is required by POSIX. On a quick test, at least GNU rm removes files given on the command line in the order listed, and doesn't sort files found during recursive operation.






    share|improve this answer























    • I looked at the GNU rm implementation and traced my way down into the fts calls from rm.c. There is one call to fts_sort but it's conditional and I'm not 100% sure what it actually does.
      – Kusalananda
      Jun 26 at 15:33











    • @Kusalananda, I can't really understand why rm would want to sort anything...
      – ilkkachu
      Jun 26 at 15:50










    • Me neither, but there it is.
      – Kusalananda
      Jun 26 at 15:54














    up vote
    1
    down vote



    accepted










    Globs are expanded by the shell in lexicographic order (in the current locale), and the rm implementation is likely to remove the files it gets as arguments in the same order it got them. So in your case the files would be removed starting with the oldest.



    Sorting the glob results is required by POSIX. On a quick test, at least GNU rm removes files given on the command line in the order listed, and doesn't sort files found during recursive operation.






    share|improve this answer























    • I looked at the GNU rm implementation and traced my way down into the fts calls from rm.c. There is one call to fts_sort but it's conditional and I'm not 100% sure what it actually does.
      – Kusalananda
      Jun 26 at 15:33











    • @Kusalananda, I can't really understand why rm would want to sort anything...
      – ilkkachu
      Jun 26 at 15:50










    • Me neither, but there it is.
      – Kusalananda
      Jun 26 at 15:54












    up vote
    1
    down vote



    accepted







    up vote
    1
    down vote



    accepted






    Globs are expanded by the shell in lexicographic order (in the current locale), and the rm implementation is likely to remove the files it gets as arguments in the same order it got them. So in your case the files would be removed starting with the oldest.



    Sorting the glob results is required by POSIX. On a quick test, at least GNU rm removes files given on the command line in the order listed, and doesn't sort files found during recursive operation.






    share|improve this answer















    Globs are expanded by the shell in lexicographic order (in the current locale), and the rm implementation is likely to remove the files it gets as arguments in the same order it got them. So in your case the files would be removed starting with the oldest.



    Sorting the glob results is required by POSIX. On a quick test, at least GNU rm removes files given on the command line in the order listed, and doesn't sort files found during recursive operation.







    share|improve this answer















    share|improve this answer



    share|improve this answer








    edited Jun 26 at 15:49


























    answered Jun 26 at 15:26









    ilkkachu

    47.3k668130




    47.3k668130











    • I looked at the GNU rm implementation and traced my way down into the fts calls from rm.c. There is one call to fts_sort but it's conditional and I'm not 100% sure what it actually does.
      – Kusalananda
      Jun 26 at 15:33











    • @Kusalananda, I can't really understand why rm would want to sort anything...
      – ilkkachu
      Jun 26 at 15:50










    • Me neither, but there it is.
      – Kusalananda
      Jun 26 at 15:54
















    • I looked at the GNU rm implementation and traced my way down into the fts calls from rm.c. There is one call to fts_sort but it's conditional and I'm not 100% sure what it actually does.
      – Kusalananda
      Jun 26 at 15:33











    • @Kusalananda, I can't really understand why rm would want to sort anything...
      – ilkkachu
      Jun 26 at 15:50










    • Me neither, but there it is.
      – Kusalananda
      Jun 26 at 15:54















    I looked at the GNU rm implementation and traced my way down into the fts calls from rm.c. There is one call to fts_sort but it's conditional and I'm not 100% sure what it actually does.
    – Kusalananda
    Jun 26 at 15:33





    I looked at the GNU rm implementation and traced my way down into the fts calls from rm.c. There is one call to fts_sort but it's conditional and I'm not 100% sure what it actually does.
    – Kusalananda
    Jun 26 at 15:33













    @Kusalananda, I can't really understand why rm would want to sort anything...
    – ilkkachu
    Jun 26 at 15:50




    @Kusalananda, I can't really understand why rm would want to sort anything...
    – ilkkachu
    Jun 26 at 15:50












    Me neither, but there it is.
    – Kusalananda
    Jun 26 at 15:54




    Me neither, but there it is.
    – Kusalananda
    Jun 26 at 15:54












    up vote
    1
    down vote













    The shell expands the expression before it executes rm, and that expansion is not specific to the command, so if you do:



    echo *20180626_1*


    Then the first thing that echos will be the first thing that the shell passes to rm. The order isn't random, it's alphabetical. From the bash man page:




    After word splitting, unless the -f option has been set, bash scans each word for the characters *, ?, and [. If one of these characters appears, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of file names matching the pattern.







    share|improve this answer



























      up vote
      1
      down vote













      The shell expands the expression before it executes rm, and that expansion is not specific to the command, so if you do:



      echo *20180626_1*


      Then the first thing that echos will be the first thing that the shell passes to rm. The order isn't random, it's alphabetical. From the bash man page:




      After word splitting, unless the -f option has been set, bash scans each word for the characters *, ?, and [. If one of these characters appears, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of file names matching the pattern.







      share|improve this answer

























        up vote
        1
        down vote










        up vote
        1
        down vote









        The shell expands the expression before it executes rm, and that expansion is not specific to the command, so if you do:



        echo *20180626_1*


        Then the first thing that echos will be the first thing that the shell passes to rm. The order isn't random, it's alphabetical. From the bash man page:




        After word splitting, unless the -f option has been set, bash scans each word for the characters *, ?, and [. If one of these characters appears, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of file names matching the pattern.







        share|improve this answer















        The shell expands the expression before it executes rm, and that expansion is not specific to the command, so if you do:



        echo *20180626_1*


        Then the first thing that echos will be the first thing that the shell passes to rm. The order isn't random, it's alphabetical. From the bash man page:




        After word splitting, unless the -f option has been set, bash scans each word for the characters *, ?, and [. If one of these characters appears, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of file names matching the pattern.








        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Jun 26 at 15:29


























        answered Jun 26 at 15:23









        Andy Dalton

        4,7111520




        4,7111520






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f452035%2fwhich-file-first-get-deleted-first-when-deleting-via-rm-f-with-a-glob-pattern%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?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay