Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
05-15-2017, 03:53 AM
|
#1
|
Member
Registered: Dec 2002
Posts: 223
Rep:
|
Deleting three oldest files if 7 days have passed since the oldest
I have this code:
Code:
#!/bin/sh
now="$(date +'%d_%m_%Y_%H_%M_%S')"
filename="thebackup_$now"
backupfolder="/home/theuser/thebackup"
fullpathbackupfile="$backupfolder/$filename"
logfile="$backupfolder/"backup_log_"$now".txt
touch $logfile
echo "mysqldump has started $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
mysqldump --user=mysqluser --password=mysqlpass --default-character-set=utf8 firstbd | gzip > "$fullpathbackupfile"first.gz
mysqldump --user=mysqluser --password=mysqlpass --default-character-set=utf8 secondbd | gzip > "$fullpathbackupfile"second.gz
echo "mysqldump finished $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
chown theuser "$fullpathbackupfile"first.gz
chown theuser "$fullpathbackupfile"second.gz
chown theuser "$logfile"
echo "i have changed perm" >> "$logfile"
find "$backupfolder" -name db_backup_* -mtime +8 -exec rm {} \;
echo "cleaning" >> "$logfile"
echo "backup has finished $(date +'%d-%m-%Y %H:%M:%S')" >> "$logfile"
echo "*****************" >> "$logfile"
exit 0
Which does what it does.
What I want to do is that if gets all the files and sees the date of the oldest. If the oldest is more tan 7 days, it should get the 3 oldest files and delete them.
|
|
|
05-15-2017, 05:33 AM
|
#2
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,724
|
|
|
|
05-15-2017, 05:35 AM
|
#3
|
Member
Registered: Dec 2002
Posts: 223
Original Poster
Rep:
|
Quote:
Originally Posted by pan64
|
What is there to indent? LOL Its all one liners.
Stop being so picky.
|
|
|
05-15-2017, 05:40 AM
|
#4
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,724
|
please follow the rules of the forum.
show us what did you try, what's happened, what is your actual problem. Do not expect anyone will write something for you.
Especially you missed point 3, 4, and 5.
|
|
|
05-15-2017, 05:43 AM
|
#5
|
Member
Registered: Dec 2002
Posts: 223
Original Poster
Rep:
|
Quote:
Originally Posted by pan64
please follow the rules of the forum.
show us what did you try, what's happened, what is your actual problem. Do not expect anyone will write something for you.
Especially you missed point 3, 4, and 5.
|
This is the newbie section.
As such, it is supposed that I do not have a clue what I am doing.
So, the question is still the same: How do I check in the directory "/home/theuser/thebackup" the date of files and if the oldest file has more than 7 days, it should delete the 3 oldest files.? Thats my problem
|
|
|
05-15-2017, 06:53 AM
|
#6
|
LQ Guru
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,518
|
You will need to use the find utility and also sort and awk or head. However, do you really want to delete the three oldest even if two of them are less than seven days old? Or do you want to delete the three oldest files that are all over seven days old?
To get started with find, see -daystart and -mtime and -printf
|
|
1 members found this post helpful.
|
05-15-2017, 07:10 AM
|
#7
|
Moderator
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,894
|
Quote:
Originally Posted by riahc3
I have this code:
Which does what it does.
|
What exactly does it do now?
Quote:
Originally Posted by riahc3
What I want to do is that if gets all the files and sees the date of the oldest. If the oldest is more tan 7 days, it should get the 3 oldest files and delete them.
|
The find command can find files of a specified age. I feel you have shown a great deal of code non-related to your question. Perhaps consider not showing the extensive code and instead just the portions where you are trying to located files of a certain age.
Is this code which you've written? What experience do you have with bash scripting? Reason I'm asking is that people will benefit by knowing whether or not you have any experience with scripting at all, or if you are trying to modify a script to accomplish these actions, but do not necessarily have great experience with bash or scripting yourself.
Suggest you put a "set -xv" line in your script to help you debug it.
|
|
|
05-15-2017, 08:39 AM
|
#8
|
LQ Veteran
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Rep:
|
Quote:
Originally Posted by riahc3
this code:
Code:
find "$backupfolder" -name db_backup_* -mtime +8 -exec rm {} \;
What I want to do is that if gets all the files and sees the date of the oldest. If the oldest is more tan 7 days, it should get the 3 oldest files and delete them.
|
Looks contradictory to me?
|
|
|
All times are GMT -5. The time now is 10:45 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|