Delete 3 oldest files

Trying to delete my 3 oldest files.

I am learning despite the many questions.

This shows the files.

ls -1r /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Ubuntu_Documents.zip_* | tail -n+6

adding this on did not work.

| -exec rm {}

------ Post updated at 05:43 PM ------

This works, but I seek advice from the gurus.

ls -1r /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Ubuntu_Documents.zip_* | tail -n+3 > To_Be_Deleted.txt
xargs rm  < To_Be_Deleted.txt

I can see a problem.

Each time it runs it will delete more files until they are all gone. :slight_smile:

------ Post updated at 09:02 PM ------

I think I need to capture the count of files, and then control execution to remove that count.

Think about your requirement.
Maybe it is better to keep a certain number of files?

You are right.

Keeping backups 3 days would be unacceptable in most organization.

Yes.
A lot is depending on being able to restore. :slight_smile:

What difference does that make?

If a criminal gets into a system or a rouge employee does bad things to data, that is normally not discovered immediately, something it might be not be discovered for many days, weeks or even months.

Three days of backup assumes that you are not concerned about any of those basic IT security topics and you just want to restore from a crash.

That's a very narrow view of the reason for making backups.

I have already told you that I am NOT an admin.

Sorry about that :wink:

No problem. :slight_smile:

You had not said anything about being an admin or about not being an admin in this thread until post #7 in this thread. Please do not assume that everyone has read all of your posts in all of the threads in which you have posted a comment when replying to one of your threads. Assume that each thread is complete on its own without needing to know what has been discussed in other threads unless a link to those other threads has been included. (This includes ALWAYS telling us what operating system and shell you're using when starting a thread in this forum.)

When you ask us how to delete the 3 oldest files in a directory, you're likely to get suggestions that will tell you how to delete the 3 oldest files in a directory (no matter how many files that leaves untouched).

If you ask us how to delete backup files that are more than six months old, you're likely to get suggestions that will tell you how to delete backup files that are more than six months (or more than 180 days) old.

Not mentioning that you're working on backup files (until post #5 in a thread) when there seems to be a hidden requirement to keep a certain number of backups or backups for a certain period of time, makes it unlikely that any early suggestions you get will be of much help.

You have a lot of context that guides you when you're trying to decide what you want to do. If you don't share that context with us when you ask for help, it is unlikely that the help we try to provide will actually help meet your unstated requirements.

Please help us help you!

1 Like

You can chain the two conditions

ls -1t /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Ubuntu_Documents.zip_* | 
# skip (leave) the 5 newest
tail -n +6 |
# show (delete) the 3 oldest
tail -n -3 |
while IFS= read -r file
do
  echo rm "$file"
done
1 Like

Ok, I will try to err on the side of providing too much info even if includes repeating previous info.

------ Post updated at 08:50 AM ------

I modified your script a little. I want to delete all but the newest 2 files.

ls -1t /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Documents_Backups/Ubuntu_Documents_* | 
# skip (leave) the 5 newest
tail -n +6 |
# show (delete) the 3 oldest
tail -n -3 |
while IFS= read -r file
do
  echo rm "$file"
done   
exit 0

This is what is in /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Documents_Backups/ when I ran script and remained the same after running the script.

-rw-rw-r-- 1 andy andy 29454294 Aug 13 07:59 Ubuntu_Documents_2018-08-13-07-59.zip
-rw-rw-r-- 1 andy andy 29454294 Aug 12 18:03 Ubuntu_Documents_2018-08-12-18-03.zip
-rw-rw-r-- 1 andy andy 29454294 Aug 12 15:22 Ubuntu_Documents_2018-08-12-15-22.zip
-rw-rw-r-- 1 andy andy        0 Jan  1  2012 Ubuntu_Documents_2018-08-12-15-40.zip

Script did not delete anything.

Because you did not modify it?

...
# skip (leave) the 2 newest
tail -n +3 |
...
ls -1t /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Documents_Backups/Ubuntu_Documents_* | 
# skip (leave) the 2 newest
tail -n +3 |
# show (delete) the 3 oldest
tail -n -3 |
rm /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Documents_Backups/Ubuntu_Documents_2018-08-12-15-22.zip
rm /media/andy/MAXTOR_SDB1/Ubuntu_Mate_18.04/Documents_Backups/Ubuntu_Documents_2018-08-12-15-40.zip


It did not delete those 2 files.

For demonstration I have put an echo before the rm .
Remove it to really run the rm !

Excellent.