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.
|
 |
03-27-2017, 06:01 AM
|
#1
|
Member
Registered: Dec 2002
Posts: 228
Rep:
|
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
|
|
|
03-27-2017, 07:16 AM
|
#3
|
LQ Guru
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 11,139
|
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.
|
|
|
03-27-2017, 09:37 AM
|
#4
|
Member
Registered: Dec 2002
Posts: 228
Original Poster
Rep:
|
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
|
|
|
03-27-2017, 09:47 AM
|
#5
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,988
|
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.
|
03-27-2017, 03:22 PM
|
#6
|
Senior Member
Registered: Mar 2010
Distribution: Slackware
Posts: 2,192
|
'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.
|
04-19-2017, 02:21 AM
|
#7
|
Member
Registered: Dec 2002
Posts: 228
Original Poster
Rep:
|
Quote:
Originally Posted by pan64
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?
|
|
|
04-20-2017, 04:41 AM
|
#8
|
Member
Registered: Dec 2002
Posts: 228
Original Poster
Rep:
|
Nothing?
|
|
|
04-24-2017, 02:16 AM
|
#9
|
Member
Registered: Dec 2002
Posts: 228
Original Poster
Rep:
|
Noone can lend a hand?
|
|
|
04-24-2017, 02:22 AM
|
#10
|
LQ Addict
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 23,988
|
you did not accept ....
|
|
1 members found this post helpful.
|
04-24-2017, 03:04 AM
|
#11
|
Member
Registered: Dec 2002
Posts: 228
Original Poster
Rep:
|
Quote:
Originally Posted by pan64
you did not accept ....
|
Did not accept what?
|
|
|
04-25-2017, 12:34 AM
|
#12
|
Senior Member
Registered: Mar 2010
Distribution: Slackware
Posts: 2,192
|
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'
|
|
|
04-25-2017, 02:33 AM
|
#13
|
Member
Registered: Dec 2002
Posts: 228
Original Poster
Rep:
|
Quote:
Originally Posted by RandomTroll
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.
|
|
|
04-25-2017, 07:02 AM
|
#14
|
LQ Guru
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 27,594
|
Quote:
Originally Posted by riahc3
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....
|
|
|
04-25-2017, 10:35 PM
|
#15
|
Senior Member
Registered: Mar 2010
Distribution: Slackware
Posts: 2,192
|
Quote:
Originally Posted by riahc3
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.
|
|
|
All times are GMT -5. The time now is 07:35 PM.
|
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
|
|