LinuxQuestions.org
Review your favorite Linux distribution.
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 03-27-2017, 06:01 AM   #1
riahc3
Member
 
Registered: Dec 2002
Posts: 176

Rep: Reputation: 1
Delete files older than x minutes/horus/days/etc.


Im currently running a script that creates two files inside a folder each time it runs.

hello($timestamphere).sql at 1020
hellolog($timestamphere) at 1021

And so on.

Id like to make a modification and if it counts the files inside the folder and it is 30, the oldest two ones should be deleted to keep things clean.

How can I do this?

Thank you
 
Old 03-27-2017, 07:00 AM   #2
kilgoretrout
Senior Member
 
Registered: Oct 2003
Posts: 2,705

Rep: Reputation: 245Reputation: 245Reputation: 245
Just to get you started, this will show you how to get the number of files in the current directory:

http://tldp.org/HOWTO/Bash-Prompt-HOWTO/x700.html

And this will show you how to find the oldest file in a directory tree:

https://superuser.com/questions/5526...directory-tree

Then it's just a matter of setting up a do loop for the condition you want and deleting the files you want.
 
1 members found this post helpful.
Old 03-27-2017, 07:16 AM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177Reputation: 3177
Perhaps the logrotate utility is just what the doctor ordered.

It can automatically compress old files, and delete them after a certain time, and do a great many other useful things. It can be applied to any directory(ies) anywhere.
 
Old 03-27-2017, 09:37 AM   #4
riahc3
Member
 
Registered: Dec 2002
Posts: 176

Original Poster
Rep: Reputation: 1
Does this look good?:

Code:
#!/bin/bash
count=`ls -1 /path/to/your/directory | wc -l`
if [ $count -ge 30 ]
then
for file in $( ls -lt /path/to/your/directory | tail -n 2)
do
    echo "Deleting File $file"
    rm $file
done
fi
 
Old 03-27-2017, 09:47 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,083

Rep: Reputation: 4138Reputation: 4138Reputation: 4138Reputation: 4138Reputation: 4138Reputation: 4138Reputation: 4138Reputation: 4138Reputation: 4138Reputation: 4138Reputation: 4138
if that works for you....

But actually it is not really good:
1. do not use:
for file in $(whatever command)
because that is unsafe, you need to use:
Code:
while read file
do
....
done << $(command)
2. do not need loop at all
rm $( ls -lt /path/to/your/directory | tail -n 2)
should also work without any loop, but probably that is unsafe too
 
1 members found this post helpful.
Old 03-27-2017, 03:22 PM   #6
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,286

Rep: Reputation: 218Reputation: 218Reputation: 218
'test' can compare files by date. When I want to do what you're doing, I create a file with the relevant date, using
Code:
touch -tTheDate TimeComparisonFile
then act on all files newer or older than that file's date.

Code:
for file in *
do
if [ $file -ot TimeComparisonFile ]
then
rm $file
fi
done
I think it odd that test can't compare a file to an actual date, but that's the way I read the man page.

Note that TheDate has to include minutes and seconds, for example 201703271237 would be 12:37 on 2017 March 27.
 
1 members found this post helpful.
Old 04-19-2017, 02:21 AM   #7
riahc3
Member
 
Registered: Dec 2002
Posts: 176

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by pan64 View Post
if that works for you....

But actually it is not really good:
1. do not use:
for file in $(whatever command)
because that is unsafe, you need to use:
Code:
while read file
do
....
done << $(command)
2. do not need loop at all
rm $( ls -lt /path/to/your/directory | tail -n 2)
should also work without any loop, but probably that is unsafe too
Your code makes no sense, sorry.

Could you please post the correct and "safe" code?
 
Old 04-20-2017, 04:41 AM   #8
riahc3
Member
 
Registered: Dec 2002
Posts: 176

Original Poster
Rep: Reputation: 1
Nothing?
 
Old 04-24-2017, 02:16 AM   #9
riahc3
Member
 
Registered: Dec 2002
Posts: 176

Original Poster
Rep: Reputation: 1
Noone can lend a hand?
 
Old 04-24-2017, 02:22 AM   #10
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 13,083

Rep: Reputation: 4138Reputation: 4138Reputation: 4138Reputation: 4138Reputation: 4138Reputation: 4138Reputation: 4138Reputation: 4138Reputation: 4138Reputation: 4138Reputation: 4138
you did not accept ....
 
1 members found this post helpful.
Old 04-24-2017, 03:04 AM   #11
riahc3
Member
 
Registered: Dec 2002
Posts: 176

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by pan64 View Post
you did not accept ....
Did not accept what?
 
Old 04-25-2017, 12:34 AM   #12
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,286

Rep: Reputation: 218Reputation: 218Reputation: 218
Mr riach3:

I do not understand what Mr pan64 means either. I recommend that you stop trying to understand his answer and pursue an alternative.

Code:
rm `ls -t | tail -2`
works if there are no other files in the directory; if the files have a unique feature to their name, that can be used in the 'ls'
 
Old 04-25-2017, 02:33 AM   #13
riahc3
Member
 
Registered: Dec 2002
Posts: 176

Original Poster
Rep: Reputation: 1
Quote:
Originally Posted by RandomTroll View Post
Mr riach3:

I do not understand what Mr pan64 means either. I recommend that you stop trying to understand his answer and pursue an alternative.

Code:
rm `ls -t | tail -2`
works if there are no other files in the directory; if the files have a unique feature to their name, that can be used in the 'ls'
Sure Im open to alternatives as long as they work 100%

Like I said the files will be


file201604182100
filelog201604182100
file201604182200
filelog201604182200
file201604182300
filelog201604182300

And so on. When it reaches 30, it should delete the last oldest set.
 
Old 04-25-2017, 07:02 AM   #14
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 21,952

Rep: Reputation: 5814Reputation: 5814Reputation: 5814Reputation: 5814Reputation: 5814Reputation: 5814Reputation: 5814Reputation: 5814Reputation: 5814Reputation: 5814Reputation: 5814
Quote:
Originally Posted by riahc3 View Post
Sure Im open to alternatives as long as they work 100% Like I said the files will be


file201604182100
filelog201604182100
file201604182200
filelog201604182200
file201604182300
filelog201604182300

And so on. When it reaches 30, it should delete the last oldest set.
Pan64 is saying "We did try to give you a hand, but you didn't accept it". riahc3, don't bump your own threads after short times, or at ALL, unless you're adding more information. You were given two links in the first reply which told you how to find the oldest files, and how to write a bash script. What, then, is preventing you from actually writing your own script?? We aren't going to 'correct' your code for you, nor post you the "work 100%" solution. Read the "Question Guidelines" link.

We are happy to HELP you with what you have personally written/done...so post that, or tell us what error(s) you're getting with what you did write. You posted a script..did you try to modify it according to what pan64 told you? Did you try to actually RUN what you wrote, to see if it worked? It's up to you to test/write your own scripts....
 
Old 04-25-2017, 10:35 PM   #15
RandomTroll
Senior Member
 
Registered: Mar 2010
Distribution: Slackware
Posts: 1,286

Rep: Reputation: 218Reputation: 218Reputation: 218
Quote:
Originally Posted by riahc3 View Post
the files will be


file201604182100
filelog201604182100
file201604182200
filelog201604182200
file201604182300
filelog201604182300

And so on. When it reaches 30, it should delete the last oldest set.
Code:
if [ `ls file* | grep -c file` -eq 30 ]
then
rm `ls -t file* | tail -2`
fi
will delete the 29th-oldest and 30th-oldest files named file* when there are 30 files named file*. If you know there will always be 30, you can do without the test.

If there are non-target files of the form file*, this won't work, you'll need extra information to distinguish the target files from innocent bystanders.
 
  


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
How to delete two days older files through script azheruddin Linux - Newbie 4 01-08-2013 12:18 PM
delete files when it's older 30 days Madison00 Linux - Newbie 8 01-07-2011 02:07 AM
[SOLVED] Delete old files older than 7 days rjo98 Linux - Newbie 3 09-18-2009 01:15 PM
Delete files older then 30 days stefaandk *BSD 1 01-07-2008 08:31 PM
delete files older than 30 days using cronjob latheesan Linux - Newbie 5 06-14-2005 02:40 PM

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

All times are GMT -5. The time now is 03:11 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration