LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 05-15-2017, 03:53 AM   #1
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Rep: Reputation: 1
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.
 
Old 05-15-2017, 05:33 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,724

Rep: Reputation: 7554Reputation: 7554Reputation: 7554Reputation: 7554Reputation: 7554Reputation: 7554Reputation: 7554Reputation: 7554Reputation: 7554Reputation: 7554Reputation: 7554
http://www.linuxquestions.org/questi...gs-4175464257/
 
Old 05-15-2017, 05:35 AM   #3
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by pan64 View Post
What is there to indent? LOL Its all one liners.

Stop being so picky.
 
Old 05-15-2017, 05:40 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,724

Rep: Reputation: 7554Reputation: 7554Reputation: 7554Reputation: 7554Reputation: 7554Reputation: 7554Reputation: 7554Reputation: 7554Reputation: 7554Reputation: 7554Reputation: 7554
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.
 
Old 05-15-2017, 05:43 AM   #5
riahc3
Member
 
Registered: Dec 2002
Posts: 223

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by pan64 View Post
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
 
Old 05-15-2017, 06:53 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,518
Blog Entries: 4

Rep: Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817Reputation: 3817
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.
Old 05-15-2017, 07:10 AM   #7
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,894
Blog Entries: 13

Rep: Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945Reputation: 4945
Quote:
Originally Posted by riahc3 View Post
I have this code:

Code:
    #!/bin/sh
Which does what it does.
What exactly does it do now?
Quote:
Originally Posted by riahc3 View Post
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.
 
Old 05-15-2017, 08:39 AM   #8
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by riahc3 View Post
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?
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script for deleting the oldest directory within a directory kauuttt Linux - Newbie 6 07-14-2013 05:11 AM
[SOLVED] oldest files will delete after reach to 21G ghaedi591 Linux - General 5 11-07-2011 10:20 AM
Creating a script that removes the oldest files on /tmp directory danndp Linux - Newbie 6 11-18-2010 09:47 AM
sort oldest 5 files in a directory tree recursively based on timestamp Linux abhelp Programming 1 06-04-2010 04:58 AM
Auto Delete oldest files @ratex when directory is 98% full jmanjohn61 Linux - Software 1 04-05-2005 03:44 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration