Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
| 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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
01-14-2013, 09:48 AM
|
#1
|
|
Member
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 500
Rep:
|
Delete files after few weeks autoamticly
Hi,
I will copy files from /test to /test2
with cron and use $date to create names. I created a bash for that.
Every week it will copy one file from /test to /test2
But I want after few weeks to delete old files and keep only last 6 ones, because of used spaces. I have 1TB and files are 100GB.
Is there any way to do it autoamticly by cron job?
Last edited by miros84; 01-14-2013 at 10:12 AM.
|
|
|
|
01-14-2013, 10:01 AM
|
#2
|
|
LQ Newbie
Registered: Jan 2013
Location: Scotland
Distribution: Debian
Posts: 16
Rep:
|
How often are you creating the files in /test2? Can you say they're being created regularly?
Let's say that within 14 days you create six files. Anything older than than can be deleted, so simple find . -mtime +14 | xargs rm should work no problem.
|
|
|
|
01-14-2013, 10:18 AM
|
#3
|
|
Member
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 500
Original Poster
Rep:
|
For example I want to delete all file in a folder /etc/test created before 40 days or more.
I dont understand really your example. Can you use code tags and create it again please?
Something like that?
Code:
-mtime +40 | xargs rm /dev/test
Last edited by miros84; 01-14-2013 at 10:21 AM.
|
|
|
|
01-14-2013, 10:22 AM
|
#4
|
|
LQ Newbie
Registered: Jan 2013
Location: Scotland
Distribution: Debian
Posts: 16
Rep:
|
Kind of:
Code:
find /dev/test -type f -mtime +40 | xargs rm
|
|
|
|
01-14-2013, 10:26 AM
|
#5
|
|
Member
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 500
Original Poster
Rep:
|
I just found another example. But which is the different?
Code:
find /dev/test -mtime +40 -exec rm {} \;
Both do the same?
|
|
|
|
01-14-2013, 10:32 AM
|
#6
|
|
LQ Newbie
Registered: Jan 2013
Location: Scotland
Distribution: Debian
Posts: 16
Rep:
|
-exec function of find executes rm on each file. With a lot of files, xargs can be noticeably more efficient. I'd use -0 switch if spaces may appear in file path/name.
As for the -type f, that's just personal choice. rm won't delete folders without -r, but type -f[ile] just makes sure folder names aren't passed to rm anyway.
I'd change your example slightly:
Code:
find /dev/test -mtime +40 -type f -exec rm {} \;
Last edited by rmacd; 01-14-2013 at 10:33 AM.
|
|
|
|
01-14-2013, 10:40 AM
|
#7
|
|
Member
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 500
Original Poster
Rep:
|
Thank you rmacd,
New un LQ but I see you have a lot of experience in Linux.
|
|
|
|
01-14-2013, 11:02 AM
|
#8
|
|
Moderator
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Slackware, Debian
Posts: 12,525
|
Quote:
Originally Posted by rmacd
-exec function of find executes rm on each file.
|
This is not quite true, how the -exec option behaves is determined by the ending character of the command. Usually you will use the ; ending character (which due to its function as a special character in the shell has to be escaped to \;), but you can also use the + ending character, which will behave similarly to xargs.
So you have several options to achieve the task, the most common would be:
1. If it is expected that the find command will find only a few files (which is the case for the OP) you can use
Code:
find /etc/test -type f -mtime +40 -exec rm '{}' \;
or
Code:
find /etc/test -type f -mtime +40 -delete
This will, as rmacd already stated, invoke the deletion on any found file separately, which will cause big overhead when many files are found.
2. If it is expected that a large number of files will be found it is better to use either
Code:
find /etc/test -type f -mtime +40 | xargs rm
or
Code:
find /etc/test -type f -mtime +40 -exec rm '{}' +
In both cases the found files will be assembled to sets of files and then the command is launched on a whole set of filenames(in the first case by the xargs command, in the second find will do the work). This will speed up the execution when large numbers of files are found.
Last edited by TobiSGD; 01-14-2013 at 11:05 AM.
Reason: Today is typo day
|
|
|
2 members found this post helpful.
|
02-04-2013, 07:06 AM
|
#9
|
|
Member
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 500
Original Poster
Rep:
|
Quote:
Originally Posted by TobiSGD
This is not quite true, how the -exec option behaves is determined by the ending character of the command. Usually you will use the ; ending character (which due to its function as a special character in the shell has to be escaped to \  , but you can also use the + ending character, which will behave similarly to xargs.
So you have several options to achieve the task, the most common would be:
1. If it is expected that the find command will find only a few files (which is the case for the OP) you can use
Code:
find /etc/test -type f -mtime +40 -exec rm '{}' \;
or
Code:
find /etc/test -type f -mtime +40 -delete
This will, as rmacd already stated, invoke the deletion on any found file separately, which will cause big overhead when many files are found.
2. If it is expected that a large number of files will be found it is better to use either
Code:
find /etc/test -type f -mtime +40 | xargs rm
or
Code:
find /etc/test -type f -mtime +40 -exec rm '{}' +
In both cases the found files will be assembled to sets of files and then the command is launched on a whole set of filenames(in the first case by the xargs command, in the second find will do the work). This will speed up the execution when large numbers of files are found.
|
I tried your examples but I have got this errors:
Code:
rm: falta un operando (miss one operating)
Pruebe `rm --help' para más información. (try `rm --help' for more information)
Any idea?
Last edited by miros84; 02-05-2013 at 02:28 AM.
|
|
|
|
02-06-2013, 04:54 AM
|
#10
|
|
Member
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 500
Original Poster
Rep:
|
Tobi,
Only this command works for me
Code:
find /etc/test -type f -mtime +40 -delete
Other ones that end with RM doesnot work.
And is there any way to add some parameter, to see what happend after command is executed?
With rsync I can add -v and I can see that happend, but with find, I dont see -v exist.
After I execute
Code:
find /etc/test -type f -mtime +40 -delete
there is nothing in the log file. It is empty.
Any idea?
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 03:33 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
|
|