Delete oldest folder based on folder named as date


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete oldest folder based on folder named as date
# 1  
Old 07-30-2018
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 looking at the list below folder 2018-01-01 is the oldest folder as per directory naming. I want the expect code code below to find and delete it.

Code:
2018-07-30
2018-07-29
2018-07-28
2018-07-27
2018-07-26
2018-07-25
.
.
.
2018-01-01




Code:
expect << 'EOF' >> $today.log
set dir [timestamp -format "%Y-%m-%d"]
spawn sftp server@ip
expect "Password:"
send "Password\r"
expect "sftp>"
send "lcd /backup/\r"
expect "sftp>"
send "cd /BackupDirectory/\r"
expect "sftp>"
send "Here I want the code to find the oldest directory as per name and delete it\r"
expect "sftp>"
send "mkdir $dir\r"
expect "sftp>"
send "put -r /backup/$dir/*.gz /BackupDirectory/$dir/\r"
set timeout -1
expect "sftp>"
send "!rm -rf /backup/$dir\r"
expect "sftp>"
send "bye\r"
EOF
}


Last edited by humble_learner; 07-31-2018 at 12:09 AM..
# 2  
Old 07-31-2018
As your directory names are suitably formatted, you can just use ls's inherent sort. Try
Code:
echo rm $(ls -1d 2018* | head -1)

The echo is for testing / debugging only.


BTW, you mayhap don't need the expect command for your purpose. Did you consider sftp's -b batchfile option and ssh's public key authentication?
This User Gave Thanks to RudiC For This Post:
# 3  
Old 07-31-2018
Hi Rudic,

Thank you for the answer, I just had to modify a bit in order to cover years past 2018 but your answer was correct.

Code:
rm -rf $(ls -ld [0-9][0-9][0-9][0-9]* | head -1


For your suggestion about -b batchfile and ssh's public key options I did looked into that and tried implementing that solution first. However, at that moment it was too complicated for me and did not worked out. I will look into that in near future. Do you have any guide on implementing that solution? Any input is further appreciated.


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 07-31-2018 at 10:02 AM.. Reason: Added CODE tags.
# 4  
Old 07-31-2018
The batchfile handling might be self-explanatory, and for ssh's public key authentication there's many a thread in these fora. The links at bottom left under "More UNIX and Linux Forum Topics You Might Find Helpful" might be helpful as well.
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep date pattern folder and zip -->delete

Hi All, OS: Redhat Linux 7.5 Shell: Bash I have some folder like this 2018-09-16 2018-09-17 2018-09-18 2018-09-19 and so on... Everyday one script create a folder with pattern YYYY-MM-DD (it will have so many sub directories files in it) Now what I would like to achieve is a... (1 Reply)
Discussion started by: onenessboy
1 Replies

2. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

3. Shell Programming and Scripting

Bash to select oldest folder in directory and write to log

In the bash below the oldest folder in a directory is selected. If there are 3folders in the directory /home/cmccabe/Desktop/NGS/test and nothing is done to them (ie. no files deleted, renamed) then the bash correctly identifies f1 as the oldest. However, if something is done to the folder then... (4 Replies)
Discussion started by: cmccabe
4 Replies

4. Shell Programming and Scripting

Bash to select oldest folder in directory automatically and log process

The `bash` below uses the oldest folder in the specified directory and logs it. The goes though an analysis process and creates a log. My problem is that if there are 3 folders in the directory folder1,folder2,folder3, the bash is using folder2 for the analysis eventhough folder1 is the oldest... (0 Replies)
Discussion started by: cmccabe
0 Replies

5. UNIX for Dummies Questions & Answers

Select all files in a folder based on creation date (ls command)

Hi All, <Re-posting in Correct group> I'm trying to select all the files in a folder that starts with a particular name format and are created in a gven date range using 'ls' command...but i'm not successful.... Example : I'm trying to see all the text files in a folder who names start... (6 Replies)
Discussion started by: Satya C1
6 Replies

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

7. Shell Programming and Scripting

Get the oldest date based on date in the filename

I am using ksh93 on Solaris. Ok, this may seem like a simple request at first. I have a directory that contains sets of files with a YYYYMMDD component to the name, along with other files of different filespecs. something like this: 20110501_1.dat 20110501_2.dat 20110501_3.dat... (2 Replies)
Discussion started by: gary_w
2 Replies

8. Shell Programming and Scripting

delete all the files in folder a which exist in folder b

Hi , I need a script which basically deltes all files in folder a which are alreasy present in folder b say folder a has files abc.txt pqr .txt and b has abc.txt pqr.txt rmr.txt then file abc.txt and pqr.txt from a should be deleted (6 Replies)
Discussion started by: viv1
6 Replies

9. Shell Programming and Scripting

Bash script to delete folder based on text file information

I have been working on a script to list all the name's of a subfolder in a text file then edit that text file and then delete the subfolder base on the edited text file so far I have been able to do every thing I just talked about but can't figure out how to delete the subfolers base on a text file... (8 Replies)
Discussion started by: bone11409
8 Replies

10. UNIX for Dummies Questions & Answers

How can i delete files in folder by date?

Hi, I have some files on a folder and i want to delete all the files that were created on July. Thanks, Kobi. (9 Replies)
Discussion started by: kobibn
9 Replies
Login or Register to Ask a Question