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?

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'
  • 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.

  • (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

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.