Delete 3 oldest files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete 3 oldest files
# 8  
Old 08-10-2018
Sorry about that Smilie
# 9  
Old 08-10-2018
No problem. :-)
# 10  
Old 08-13-2018
Quote:
Originally Posted by drew77
I have already told you that I am NOT an admin.
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.)
Quote:
Originally Posted by drew77
Yes.
A lot is depending on being able to restore. :-)
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!
This User Gave Thanks to Don Cragun For This Post:
# 11  
Old 08-13-2018
You can chain the two conditions
Code:
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

This User Gave Thanks to MadeInGermany For This Post:
# 12  
Old 08-13-2018
Quote:
Originally Posted by Don Cragun
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!

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

Quote:
Originally Posted by MadeInGermany
You can chain the two conditions
Code:
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


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

Code:
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.

Code:
-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.
# 13  
Old 08-13-2018
Because you did not modify it?
Code:
...
# skip (leave) the 2 newest
tail -n +3 |
...

# 14  
Old 08-13-2018
Code:
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 |

Code:
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete oldest folder based on folder named as date

Hi, I have a script doing backup to synology server, the script create new folder each day with the date as being folder name i.e. 2018-07-30. Just before creating the new folder I want the script to find the oldest folder from the list and delete it including its content. for example... (3 Replies)
Discussion started by: humble_learner
3 Replies

2. UNIX for Advanced & Expert Users

Finding oldest files

There are some 25,000 files in 7,000 directories in my source library and I am trying to find oldest files. I am running this find: find /usr/mysrc -name "*." -type f -mtime +8000 -exec ls -l {} 2>/dev/null and playing with the days parameter for mtime, but the output is not sorted... (3 Replies)
Discussion started by: migurus
3 Replies

3. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

4. Shell Programming and Scripting

Keeping oldest backup files?

I need a script to clean up the files on our backup system. I was hoping this would be simple for someone to put together for me. I'm sure I could do it, but I'm a bash n00b so it would definitely not be efficiently or within a reasonable amount of time. :( Requirements: - Root of backups... (3 Replies)
Discussion started by: Calab
3 Replies

5. UNIX for Dummies Questions & Answers

To delete the oldest files in a file when file count in the folder exceeds 7

Hi All, I need to delete the oldest file in folder when the file count in the folder exceed 6 ( i have a process that puts the source files into this folder ) E.x : Folder : /data/opt/backup 01/01/2012 a.txt 01/02/2012 b.txt ... (1 Reply)
Discussion started by: akshay01987
1 Replies

6. Shell Programming and Scripting

Moving files only by oldest file one at a time

Hi I am want to create a script where the file gets moved from the current folder to a folder transfer based on the oldest first. This script should run one file at a time using a loop. I want it as a loop because I want to do some processing while I have one file. Can anyone guide me on this? (2 Replies)
Discussion started by: chamajid
2 Replies

7. Shell Programming and Scripting

Need help creating a script to FTP files to a server and then delete the files that were transfered.

I am trying to FTP files to a Windows server through my Linux machine. I have setup the file transfer with no problems but am having problem deleting those files from the Linux box. My current non-working solution is below. Any ideas, anyone?? :wall: Please be gentle, I'm fairly new to this... (4 Replies)
Discussion started by: jmalfhs
4 Replies

8. Shell Programming and Scripting

delete oldest images from remote server

Would someone please give me a script that will remove all but the newest jpg image from my server. i am uploading a new image every 10 seconds form a live web cam Also i do not know who to make a shell script work i am new to all this so i need all the help you could give Thank You very much (1 Reply)
Discussion started by: Destined
1 Replies

9. Shell Programming and Scripting

sort files by date, delete oldest, if total size bigger than

hello people i need your help please i want to achieve the following with the simplest, most efficient shell-tools: i have a directory with a lot of files from users. the script should check which partition the dir is on if the partition with the directory is more than 90% full ... (2 Replies)
Discussion started by: scarfake
2 Replies

10. Shell Programming and Scripting

Oldest files

Anyone know of a way to list all of the files including subdirectories and list them as oldest first? (2 Replies)
Discussion started by: 2dumb
2 Replies
Login or Register to Ask a Question