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

Reply
 
Search this Thread
Old 01-14-2013, 09:48 AM   #1
miros84
Member
 
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 501

Rep: Reputation: 31
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.
 
Old 01-14-2013, 10:01 AM   #2
rmacd
LQ Newbie
 
Registered: Jan 2013
Location: Scotland
Distribution: Debian
Posts: 16

Rep: Reputation: 2
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.
 
Old 01-14-2013, 10:18 AM   #3
miros84
Member
 
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 501

Original Poster
Rep: Reputation: 31
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.
 
Old 01-14-2013, 10:22 AM   #4
rmacd
LQ Newbie
 
Registered: Jan 2013
Location: Scotland
Distribution: Debian
Posts: 16

Rep: Reputation: 2
Kind of:

Code:
find /dev/test -type f -mtime +40 | xargs rm
 
Old 01-14-2013, 10:26 AM   #5
miros84
Member
 
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 501

Original Poster
Rep: Reputation: 31
I just found another example. But which is the different?

Code:
find /dev/test -mtime +40 -exec rm {} \;
Both do the same?
 
Old 01-14-2013, 10:32 AM   #6
rmacd
LQ Newbie
 
Registered: Jan 2013
Location: Scotland
Distribution: Debian
Posts: 16

Rep: Reputation: 2
-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.
 
Old 01-14-2013, 10:40 AM   #7
miros84
Member
 
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 501

Original Poster
Rep: Reputation: 31
Thank you rmacd,

New un LQ but I see you have a lot of experience in Linux.
 
Old 01-14-2013, 11:02 AM   #8
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Hanover, Germany
Distribution: Main: Gentoo Others: What fits the task
Posts: 15,592
Blog Entries: 2

Rep: Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047Reputation: 4047
Quote:
Originally Posted by rmacd View Post
-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.
Old 02-04-2013, 07:06 AM   #9
miros84
Member
 
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 501

Original Poster
Rep: Reputation: 31
Quote:
Originally Posted by TobiSGD View Post
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.
 
Old 02-06-2013, 04:54 AM   #10
miros84
Member
 
Registered: Aug 2009
Location: Spain
Distribution: Debian stable, squeeze
Posts: 501

Original Poster
Rep: Reputation: 31
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?
 
  


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
[SOLVED] Extremely slow to move files to trash and/or delete files permanently... CT_0000 Linux - Newbie 2 01-09-2014 10:06 AM
How to delete files keeping the files listed in a text file -urgent jeesun Linux - General 4 10-21-2011 11:28 AM
need to rsync only selected files (--files-from) also need to delete files on dest. ? BrianK Linux - General 5 10-22-2009 09:52 PM
Can I delete files in /mnt/tmp? and Files in the trash can will not delete? M$ISBS Slackware 15 10-02-2009 11:56 PM
How to delete files that won't delete? di11rod Linux - Security 7 10-19-2005 09:14 PM


All times are GMT -5. The time now is 12:34 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration