How can I delete all but the newest 10 hidden folders in a directory












1















I backup .mozilla every (re)boot to a folder ~/nas-backups/mozilla but I only want to keep the most recent 10 folders alphabetically. The backups are stored with a date stamp e.g. .mozilla_2018_11_05_08:13



I want to delete all but the most recent 10 as part of a script but as they are hidden files I can not find a way to do it:



rm -R 'ls -t | tail -n +11' as it ignores hidden files/folders.



I have tried rm -R 'ls -ta | tail -n +11' but that doesn't work either.



If it could be expanded so it only kept the first backup of each day that would be a bonus.



May I request an explanation of any suggestions? Thanks.



Any help please?










share|improve this question





























    1















    I backup .mozilla every (re)boot to a folder ~/nas-backups/mozilla but I only want to keep the most recent 10 folders alphabetically. The backups are stored with a date stamp e.g. .mozilla_2018_11_05_08:13



    I want to delete all but the most recent 10 as part of a script but as they are hidden files I can not find a way to do it:



    rm -R 'ls -t | tail -n +11' as it ignores hidden files/folders.



    I have tried rm -R 'ls -ta | tail -n +11' but that doesn't work either.



    If it could be expanded so it only kept the first backup of each day that would be a bonus.



    May I request an explanation of any suggestions? Thanks.



    Any help please?










    share|improve this question



























      1












      1








      1








      I backup .mozilla every (re)boot to a folder ~/nas-backups/mozilla but I only want to keep the most recent 10 folders alphabetically. The backups are stored with a date stamp e.g. .mozilla_2018_11_05_08:13



      I want to delete all but the most recent 10 as part of a script but as they are hidden files I can not find a way to do it:



      rm -R 'ls -t | tail -n +11' as it ignores hidden files/folders.



      I have tried rm -R 'ls -ta | tail -n +11' but that doesn't work either.



      If it could be expanded so it only kept the first backup of each day that would be a bonus.



      May I request an explanation of any suggestions? Thanks.



      Any help please?










      share|improve this question
















      I backup .mozilla every (re)boot to a folder ~/nas-backups/mozilla but I only want to keep the most recent 10 folders alphabetically. The backups are stored with a date stamp e.g. .mozilla_2018_11_05_08:13



      I want to delete all but the most recent 10 as part of a script but as they are hidden files I can not find a way to do it:



      rm -R 'ls -t | tail -n +11' as it ignores hidden files/folders.



      I have tried rm -R 'ls -ta | tail -n +11' but that doesn't work either.



      If it could be expanded so it only kept the first backup of each day that would be a bonus.



      May I request an explanation of any suggestions? Thanks.



      Any help please?







      directory delete






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 8 at 21:19







      David

















      asked Jan 8 at 20:56









      DavidDavid

      104




      104






















          2 Answers
          2






          active

          oldest

          votes


















          1















          • Not for names with spaces or newline characters.


          • OK in your case where the names are like ".mozilla_2018_11_05_08:13 ".


          • I presumed that there are no other files or folders in the mother folder ~/nas-backups/mozilla



          • Tested it before posting:



            cd ~/nas-backups/mozilla
            total=$(ls -1A|wc -l); remove=$((total - 10)); rm -rv $(ls -1vA|head -n $remove)



          explaination



          total=$(ls -1A|wc -l) #total number of folders
          remove=$((total - 10)) #total -10 so that 10 folders are not removed
          rm -rv $(ls -1vA|head -n $remove) # removes total -10 from top(head)
          # because older folders(by name) are first in ls -1vA (v switch lists the names
          # proper numeric order.



          ls --help



          -v natural sort of (version) numbers within text




          Example:



          :~/nas-backups/mozilla$ ls -1vA
          .mozilla_2019_01_09_01:16
          .mozilla_2019_01_09_18:12
          .mozilla_2019_01_09_18:16
          .mozilla_2019_01_09_19:16
          .mozilla_2019_01_09_20:16
          .mozilla_2019_01_10_01:16
          .mozilla_2019_01_10_18:12
          .mozilla_2019_01_10_18:16
          .mozilla_2019_01_10_19:16
          .mozilla_2019_01_10_20:16
          .mozilla_2019_02_09_02:16
          .mozilla_2019_02_09_18:12
          .mozilla_2019_02_09_18:16
          .mozilla_2019_02_09_19:16
          .mozilla_2019_02_09_20:16
          .mozilla_2019_03_09_03:16
          .mozilla_2019_03_09_18:12
          .mozilla_2019_03_09_18:16
          .mozilla_2019_03_09_19:16
          .mozilla_2019_03_09_20:16
          .mozilla_2019_03_10_03:16
          .mozilla_2019_03_10_18:12
          .mozilla_2019_03_10_18:16
          .mozilla_2019_03_10_19:16
          .mozilla_2019_03_10_20:16
          :~/nas-backups/mozilla$ total=$(ls -1A|wc -l); remove=$((total - 10)); rm -rv $(ls -1vA|head -n $remove)
          removed directory '.mozilla_2019_01_09_01:16'
          removed directory '.mozilla_2019_01_09_18:12'
          removed directory '.mozilla_2019_01_09_18:16'
          removed directory '.mozilla_2019_01_09_19:16'
          removed directory '.mozilla_2019_01_09_20:16'
          removed directory '.mozilla_2019_01_10_01:16'
          removed directory '.mozilla_2019_01_10_18:12'
          removed directory '.mozilla_2019_01_10_18:16'
          removed directory '.mozilla_2019_01_10_19:16'
          removed directory '.mozilla_2019_01_10_20:16'
          removed directory '.mozilla_2019_02_09_02:16'
          removed directory '.mozilla_2019_02_09_18:12'
          removed directory '.mozilla_2019_02_09_18:16'
          removed directory '.mozilla_2019_02_09_19:16'
          removed directory '.mozilla_2019_02_09_20:16'





          share|improve this answer


























          • This works perfectly, thank you.Thanks for the explanation.

            – David
            Jan 9 at 21:45













          • I put in total=$(ls -1A|wc -l) If total > 11 as a test in case there are less than 10 - if there are everything is deleted.

            – David
            Jan 10 at 23:43





















          1














          Try this:



          ls -tA|tail -n +11|xargs rm -R


          This deletes all but the latest 10 files or folders (by file modification time) in a simple one-liner.



          ls -tA lists all files including hidden ones, but without the . and .. special files, sorted by modification time (newest first).



          tail -n +11 then takes only those files starting from the 11th entry (i.e. all but the 10 newest).



          xargs just takes the output from tail and uses it as arguments to rm (executing rm once for each line from tail). Useful for commands that don't work with a simple pipe.






          share|improve this answer


























          • (Sorry for editing your reply - new to this) Doesn't that do it on modification date date rather than alphabetically?

            – David
            Jan 9 at 2:45













          • @David Yes it does it on file time stamp rather than date, which is what I assumed you wanted since your original post uses -t as well, and it does the same thing anyway. Anything else wouldn't be possible in a single line I'm afraid.

            – Sebastian
            Jan 9 at 19:00











          • @Sebastian Thanks for replying. I will try to express myself more clearly in future. I appreciate your explasnation - thanks,

            – David
            Jan 9 at 21:48











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "89"
          };
          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%2faskubuntu.com%2fquestions%2f1108109%2fhow-can-i-delete-all-but-the-newest-10-hidden-folders-in-a-directory%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









          1















          • Not for names with spaces or newline characters.


          • OK in your case where the names are like ".mozilla_2018_11_05_08:13 ".


          • I presumed that there are no other files or folders in the mother folder ~/nas-backups/mozilla



          • Tested it before posting:



            cd ~/nas-backups/mozilla
            total=$(ls -1A|wc -l); remove=$((total - 10)); rm -rv $(ls -1vA|head -n $remove)



          explaination



          total=$(ls -1A|wc -l) #total number of folders
          remove=$((total - 10)) #total -10 so that 10 folders are not removed
          rm -rv $(ls -1vA|head -n $remove) # removes total -10 from top(head)
          # because older folders(by name) are first in ls -1vA (v switch lists the names
          # proper numeric order.



          ls --help



          -v natural sort of (version) numbers within text




          Example:



          :~/nas-backups/mozilla$ ls -1vA
          .mozilla_2019_01_09_01:16
          .mozilla_2019_01_09_18:12
          .mozilla_2019_01_09_18:16
          .mozilla_2019_01_09_19:16
          .mozilla_2019_01_09_20:16
          .mozilla_2019_01_10_01:16
          .mozilla_2019_01_10_18:12
          .mozilla_2019_01_10_18:16
          .mozilla_2019_01_10_19:16
          .mozilla_2019_01_10_20:16
          .mozilla_2019_02_09_02:16
          .mozilla_2019_02_09_18:12
          .mozilla_2019_02_09_18:16
          .mozilla_2019_02_09_19:16
          .mozilla_2019_02_09_20:16
          .mozilla_2019_03_09_03:16
          .mozilla_2019_03_09_18:12
          .mozilla_2019_03_09_18:16
          .mozilla_2019_03_09_19:16
          .mozilla_2019_03_09_20:16
          .mozilla_2019_03_10_03:16
          .mozilla_2019_03_10_18:12
          .mozilla_2019_03_10_18:16
          .mozilla_2019_03_10_19:16
          .mozilla_2019_03_10_20:16
          :~/nas-backups/mozilla$ total=$(ls -1A|wc -l); remove=$((total - 10)); rm -rv $(ls -1vA|head -n $remove)
          removed directory '.mozilla_2019_01_09_01:16'
          removed directory '.mozilla_2019_01_09_18:12'
          removed directory '.mozilla_2019_01_09_18:16'
          removed directory '.mozilla_2019_01_09_19:16'
          removed directory '.mozilla_2019_01_09_20:16'
          removed directory '.mozilla_2019_01_10_01:16'
          removed directory '.mozilla_2019_01_10_18:12'
          removed directory '.mozilla_2019_01_10_18:16'
          removed directory '.mozilla_2019_01_10_19:16'
          removed directory '.mozilla_2019_01_10_20:16'
          removed directory '.mozilla_2019_02_09_02:16'
          removed directory '.mozilla_2019_02_09_18:12'
          removed directory '.mozilla_2019_02_09_18:16'
          removed directory '.mozilla_2019_02_09_19:16'
          removed directory '.mozilla_2019_02_09_20:16'





          share|improve this answer


























          • This works perfectly, thank you.Thanks for the explanation.

            – David
            Jan 9 at 21:45













          • I put in total=$(ls -1A|wc -l) If total > 11 as a test in case there are less than 10 - if there are everything is deleted.

            – David
            Jan 10 at 23:43


















          1















          • Not for names with spaces or newline characters.


          • OK in your case where the names are like ".mozilla_2018_11_05_08:13 ".


          • I presumed that there are no other files or folders in the mother folder ~/nas-backups/mozilla



          • Tested it before posting:



            cd ~/nas-backups/mozilla
            total=$(ls -1A|wc -l); remove=$((total - 10)); rm -rv $(ls -1vA|head -n $remove)



          explaination



          total=$(ls -1A|wc -l) #total number of folders
          remove=$((total - 10)) #total -10 so that 10 folders are not removed
          rm -rv $(ls -1vA|head -n $remove) # removes total -10 from top(head)
          # because older folders(by name) are first in ls -1vA (v switch lists the names
          # proper numeric order.



          ls --help



          -v natural sort of (version) numbers within text




          Example:



          :~/nas-backups/mozilla$ ls -1vA
          .mozilla_2019_01_09_01:16
          .mozilla_2019_01_09_18:12
          .mozilla_2019_01_09_18:16
          .mozilla_2019_01_09_19:16
          .mozilla_2019_01_09_20:16
          .mozilla_2019_01_10_01:16
          .mozilla_2019_01_10_18:12
          .mozilla_2019_01_10_18:16
          .mozilla_2019_01_10_19:16
          .mozilla_2019_01_10_20:16
          .mozilla_2019_02_09_02:16
          .mozilla_2019_02_09_18:12
          .mozilla_2019_02_09_18:16
          .mozilla_2019_02_09_19:16
          .mozilla_2019_02_09_20:16
          .mozilla_2019_03_09_03:16
          .mozilla_2019_03_09_18:12
          .mozilla_2019_03_09_18:16
          .mozilla_2019_03_09_19:16
          .mozilla_2019_03_09_20:16
          .mozilla_2019_03_10_03:16
          .mozilla_2019_03_10_18:12
          .mozilla_2019_03_10_18:16
          .mozilla_2019_03_10_19:16
          .mozilla_2019_03_10_20:16
          :~/nas-backups/mozilla$ total=$(ls -1A|wc -l); remove=$((total - 10)); rm -rv $(ls -1vA|head -n $remove)
          removed directory '.mozilla_2019_01_09_01:16'
          removed directory '.mozilla_2019_01_09_18:12'
          removed directory '.mozilla_2019_01_09_18:16'
          removed directory '.mozilla_2019_01_09_19:16'
          removed directory '.mozilla_2019_01_09_20:16'
          removed directory '.mozilla_2019_01_10_01:16'
          removed directory '.mozilla_2019_01_10_18:12'
          removed directory '.mozilla_2019_01_10_18:16'
          removed directory '.mozilla_2019_01_10_19:16'
          removed directory '.mozilla_2019_01_10_20:16'
          removed directory '.mozilla_2019_02_09_02:16'
          removed directory '.mozilla_2019_02_09_18:12'
          removed directory '.mozilla_2019_02_09_18:16'
          removed directory '.mozilla_2019_02_09_19:16'
          removed directory '.mozilla_2019_02_09_20:16'





          share|improve this answer


























          • This works perfectly, thank you.Thanks for the explanation.

            – David
            Jan 9 at 21:45













          • I put in total=$(ls -1A|wc -l) If total > 11 as a test in case there are less than 10 - if there are everything is deleted.

            – David
            Jan 10 at 23:43
















          1












          1








          1








          • Not for names with spaces or newline characters.


          • OK in your case where the names are like ".mozilla_2018_11_05_08:13 ".


          • I presumed that there are no other files or folders in the mother folder ~/nas-backups/mozilla



          • Tested it before posting:



            cd ~/nas-backups/mozilla
            total=$(ls -1A|wc -l); remove=$((total - 10)); rm -rv $(ls -1vA|head -n $remove)



          explaination



          total=$(ls -1A|wc -l) #total number of folders
          remove=$((total - 10)) #total -10 so that 10 folders are not removed
          rm -rv $(ls -1vA|head -n $remove) # removes total -10 from top(head)
          # because older folders(by name) are first in ls -1vA (v switch lists the names
          # proper numeric order.



          ls --help



          -v natural sort of (version) numbers within text




          Example:



          :~/nas-backups/mozilla$ ls -1vA
          .mozilla_2019_01_09_01:16
          .mozilla_2019_01_09_18:12
          .mozilla_2019_01_09_18:16
          .mozilla_2019_01_09_19:16
          .mozilla_2019_01_09_20:16
          .mozilla_2019_01_10_01:16
          .mozilla_2019_01_10_18:12
          .mozilla_2019_01_10_18:16
          .mozilla_2019_01_10_19:16
          .mozilla_2019_01_10_20:16
          .mozilla_2019_02_09_02:16
          .mozilla_2019_02_09_18:12
          .mozilla_2019_02_09_18:16
          .mozilla_2019_02_09_19:16
          .mozilla_2019_02_09_20:16
          .mozilla_2019_03_09_03:16
          .mozilla_2019_03_09_18:12
          .mozilla_2019_03_09_18:16
          .mozilla_2019_03_09_19:16
          .mozilla_2019_03_09_20:16
          .mozilla_2019_03_10_03:16
          .mozilla_2019_03_10_18:12
          .mozilla_2019_03_10_18:16
          .mozilla_2019_03_10_19:16
          .mozilla_2019_03_10_20:16
          :~/nas-backups/mozilla$ total=$(ls -1A|wc -l); remove=$((total - 10)); rm -rv $(ls -1vA|head -n $remove)
          removed directory '.mozilla_2019_01_09_01:16'
          removed directory '.mozilla_2019_01_09_18:12'
          removed directory '.mozilla_2019_01_09_18:16'
          removed directory '.mozilla_2019_01_09_19:16'
          removed directory '.mozilla_2019_01_09_20:16'
          removed directory '.mozilla_2019_01_10_01:16'
          removed directory '.mozilla_2019_01_10_18:12'
          removed directory '.mozilla_2019_01_10_18:16'
          removed directory '.mozilla_2019_01_10_19:16'
          removed directory '.mozilla_2019_01_10_20:16'
          removed directory '.mozilla_2019_02_09_02:16'
          removed directory '.mozilla_2019_02_09_18:12'
          removed directory '.mozilla_2019_02_09_18:16'
          removed directory '.mozilla_2019_02_09_19:16'
          removed directory '.mozilla_2019_02_09_20:16'





          share|improve this answer
















          • Not for names with spaces or newline characters.


          • OK in your case where the names are like ".mozilla_2018_11_05_08:13 ".


          • I presumed that there are no other files or folders in the mother folder ~/nas-backups/mozilla



          • Tested it before posting:



            cd ~/nas-backups/mozilla
            total=$(ls -1A|wc -l); remove=$((total - 10)); rm -rv $(ls -1vA|head -n $remove)



          explaination



          total=$(ls -1A|wc -l) #total number of folders
          remove=$((total - 10)) #total -10 so that 10 folders are not removed
          rm -rv $(ls -1vA|head -n $remove) # removes total -10 from top(head)
          # because older folders(by name) are first in ls -1vA (v switch lists the names
          # proper numeric order.



          ls --help



          -v natural sort of (version) numbers within text




          Example:



          :~/nas-backups/mozilla$ ls -1vA
          .mozilla_2019_01_09_01:16
          .mozilla_2019_01_09_18:12
          .mozilla_2019_01_09_18:16
          .mozilla_2019_01_09_19:16
          .mozilla_2019_01_09_20:16
          .mozilla_2019_01_10_01:16
          .mozilla_2019_01_10_18:12
          .mozilla_2019_01_10_18:16
          .mozilla_2019_01_10_19:16
          .mozilla_2019_01_10_20:16
          .mozilla_2019_02_09_02:16
          .mozilla_2019_02_09_18:12
          .mozilla_2019_02_09_18:16
          .mozilla_2019_02_09_19:16
          .mozilla_2019_02_09_20:16
          .mozilla_2019_03_09_03:16
          .mozilla_2019_03_09_18:12
          .mozilla_2019_03_09_18:16
          .mozilla_2019_03_09_19:16
          .mozilla_2019_03_09_20:16
          .mozilla_2019_03_10_03:16
          .mozilla_2019_03_10_18:12
          .mozilla_2019_03_10_18:16
          .mozilla_2019_03_10_19:16
          .mozilla_2019_03_10_20:16
          :~/nas-backups/mozilla$ total=$(ls -1A|wc -l); remove=$((total - 10)); rm -rv $(ls -1vA|head -n $remove)
          removed directory '.mozilla_2019_01_09_01:16'
          removed directory '.mozilla_2019_01_09_18:12'
          removed directory '.mozilla_2019_01_09_18:16'
          removed directory '.mozilla_2019_01_09_19:16'
          removed directory '.mozilla_2019_01_09_20:16'
          removed directory '.mozilla_2019_01_10_01:16'
          removed directory '.mozilla_2019_01_10_18:12'
          removed directory '.mozilla_2019_01_10_18:16'
          removed directory '.mozilla_2019_01_10_19:16'
          removed directory '.mozilla_2019_01_10_20:16'
          removed directory '.mozilla_2019_02_09_02:16'
          removed directory '.mozilla_2019_02_09_18:12'
          removed directory '.mozilla_2019_02_09_18:16'
          removed directory '.mozilla_2019_02_09_19:16'
          removed directory '.mozilla_2019_02_09_20:16'






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 9 at 14:50

























          answered Jan 9 at 14:12









          VijayVijay

          1,5851718




          1,5851718













          • This works perfectly, thank you.Thanks for the explanation.

            – David
            Jan 9 at 21:45













          • I put in total=$(ls -1A|wc -l) If total > 11 as a test in case there are less than 10 - if there are everything is deleted.

            – David
            Jan 10 at 23:43





















          • This works perfectly, thank you.Thanks for the explanation.

            – David
            Jan 9 at 21:45













          • I put in total=$(ls -1A|wc -l) If total > 11 as a test in case there are less than 10 - if there are everything is deleted.

            – David
            Jan 10 at 23:43



















          This works perfectly, thank you.Thanks for the explanation.

          – David
          Jan 9 at 21:45







          This works perfectly, thank you.Thanks for the explanation.

          – David
          Jan 9 at 21:45















          I put in total=$(ls -1A|wc -l) If total > 11 as a test in case there are less than 10 - if there are everything is deleted.

          – David
          Jan 10 at 23:43







          I put in total=$(ls -1A|wc -l) If total > 11 as a test in case there are less than 10 - if there are everything is deleted.

          – David
          Jan 10 at 23:43















          1














          Try this:



          ls -tA|tail -n +11|xargs rm -R


          This deletes all but the latest 10 files or folders (by file modification time) in a simple one-liner.



          ls -tA lists all files including hidden ones, but without the . and .. special files, sorted by modification time (newest first).



          tail -n +11 then takes only those files starting from the 11th entry (i.e. all but the 10 newest).



          xargs just takes the output from tail and uses it as arguments to rm (executing rm once for each line from tail). Useful for commands that don't work with a simple pipe.






          share|improve this answer


























          • (Sorry for editing your reply - new to this) Doesn't that do it on modification date date rather than alphabetically?

            – David
            Jan 9 at 2:45













          • @David Yes it does it on file time stamp rather than date, which is what I assumed you wanted since your original post uses -t as well, and it does the same thing anyway. Anything else wouldn't be possible in a single line I'm afraid.

            – Sebastian
            Jan 9 at 19:00











          • @Sebastian Thanks for replying. I will try to express myself more clearly in future. I appreciate your explasnation - thanks,

            – David
            Jan 9 at 21:48
















          1














          Try this:



          ls -tA|tail -n +11|xargs rm -R


          This deletes all but the latest 10 files or folders (by file modification time) in a simple one-liner.



          ls -tA lists all files including hidden ones, but without the . and .. special files, sorted by modification time (newest first).



          tail -n +11 then takes only those files starting from the 11th entry (i.e. all but the 10 newest).



          xargs just takes the output from tail and uses it as arguments to rm (executing rm once for each line from tail). Useful for commands that don't work with a simple pipe.






          share|improve this answer


























          • (Sorry for editing your reply - new to this) Doesn't that do it on modification date date rather than alphabetically?

            – David
            Jan 9 at 2:45













          • @David Yes it does it on file time stamp rather than date, which is what I assumed you wanted since your original post uses -t as well, and it does the same thing anyway. Anything else wouldn't be possible in a single line I'm afraid.

            – Sebastian
            Jan 9 at 19:00











          • @Sebastian Thanks for replying. I will try to express myself more clearly in future. I appreciate your explasnation - thanks,

            – David
            Jan 9 at 21:48














          1












          1








          1







          Try this:



          ls -tA|tail -n +11|xargs rm -R


          This deletes all but the latest 10 files or folders (by file modification time) in a simple one-liner.



          ls -tA lists all files including hidden ones, but without the . and .. special files, sorted by modification time (newest first).



          tail -n +11 then takes only those files starting from the 11th entry (i.e. all but the 10 newest).



          xargs just takes the output from tail and uses it as arguments to rm (executing rm once for each line from tail). Useful for commands that don't work with a simple pipe.






          share|improve this answer















          Try this:



          ls -tA|tail -n +11|xargs rm -R


          This deletes all but the latest 10 files or folders (by file modification time) in a simple one-liner.



          ls -tA lists all files including hidden ones, but without the . and .. special files, sorted by modification time (newest first).



          tail -n +11 then takes only those files starting from the 11th entry (i.e. all but the 10 newest).



          xargs just takes the output from tail and uses it as arguments to rm (executing rm once for each line from tail). Useful for commands that don't work with a simple pipe.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 9 at 19:25

























          answered Jan 8 at 22:38









          SebastianSebastian

          1215




          1215













          • (Sorry for editing your reply - new to this) Doesn't that do it on modification date date rather than alphabetically?

            – David
            Jan 9 at 2:45













          • @David Yes it does it on file time stamp rather than date, which is what I assumed you wanted since your original post uses -t as well, and it does the same thing anyway. Anything else wouldn't be possible in a single line I'm afraid.

            – Sebastian
            Jan 9 at 19:00











          • @Sebastian Thanks for replying. I will try to express myself more clearly in future. I appreciate your explasnation - thanks,

            – David
            Jan 9 at 21:48



















          • (Sorry for editing your reply - new to this) Doesn't that do it on modification date date rather than alphabetically?

            – David
            Jan 9 at 2:45













          • @David Yes it does it on file time stamp rather than date, which is what I assumed you wanted since your original post uses -t as well, and it does the same thing anyway. Anything else wouldn't be possible in a single line I'm afraid.

            – Sebastian
            Jan 9 at 19:00











          • @Sebastian Thanks for replying. I will try to express myself more clearly in future. I appreciate your explasnation - thanks,

            – David
            Jan 9 at 21:48

















          (Sorry for editing your reply - new to this) Doesn't that do it on modification date date rather than alphabetically?

          – David
          Jan 9 at 2:45







          (Sorry for editing your reply - new to this) Doesn't that do it on modification date date rather than alphabetically?

          – David
          Jan 9 at 2:45















          @David Yes it does it on file time stamp rather than date, which is what I assumed you wanted since your original post uses -t as well, and it does the same thing anyway. Anything else wouldn't be possible in a single line I'm afraid.

          – Sebastian
          Jan 9 at 19:00





          @David Yes it does it on file time stamp rather than date, which is what I assumed you wanted since your original post uses -t as well, and it does the same thing anyway. Anything else wouldn't be possible in a single line I'm afraid.

          – Sebastian
          Jan 9 at 19:00













          @Sebastian Thanks for replying. I will try to express myself more clearly in future. I appreciate your explasnation - thanks,

          – David
          Jan 9 at 21:48





          @Sebastian Thanks for replying. I will try to express myself more clearly in future. I appreciate your explasnation - thanks,

          – David
          Jan 9 at 21:48


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Ask Ubuntu!


          • 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%2faskubuntu.com%2fquestions%2f1108109%2fhow-can-i-delete-all-but-the-newest-10-hidden-folders-in-a-directory%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

          flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

          Mangá

          Eduardo VII do Reino Unido