LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-20-2012, 02:59 AM   #1
ankitpandey
Member
 
Registered: Jan 2012
Location: Mumbai
Posts: 63

Rep: Reputation: Disabled
Script to move/delete aged files


Hey all,

i want to create a script which can delete the files older than 7 days but in a slot of 1000. As i have millions of file to delete which i need to perform 1000 files in one slot then next 1000 files and so on. The start state should be 7 days before the present date. I tried deleting all the files after 7 days but it has thrown the below error so want to do it in slot of 1000 files.

error : /usr/bin/find: arg list too long
usage: rm [-fiRr] file ...

Please suggest on this.

Thanks,
Ankit

Last edited by onebuck; 01-20-2012 at 08:35 AM.
 
Old 01-23-2012, 12:54 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
What is the command you tried using? You should be able to use the find command's "-exec" option rather than trying to append all matching files to rm through xargs (or some other similar approach).
 
Old 01-23-2012, 01:22 AM   #3
ankitpandey
Member
 
Registered: Jan 2012
Location: Mumbai
Posts: 63

Original Poster
Rep: Reputation: Disabled
Below is the command which i am using

find . -type f -mtime +7 | xargs rm
 
Old 01-23-2012, 01:32 AM   #4
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Try:
Code:
find . -type f -mtime +7 -exec rm {} \;
Understand the above command will delete files one-at-a-time as matching files are found.

You can also try this script:
Code:
#!/bin/bash

find . -type f -mtime + 7 | \
while read filename ; do
  echo "rm \"${filename}\""
  #rm "${filename}"
done
I have not tested the above script. You need to do that yourself if you want to use it.

The script should display the rm commands it would execute. Then, if you are satisfied the script is finding the right files (and will execute the correct rm command), then remove the comment mark ('#') from the rm command and re-run the script to actually delete the files. Keep in mind, that you must run the script from the top directory you wish to work from--because the find command uses the '.' for the path.

Last edited by Dark_Helmet; 01-23-2012 at 01:33 AM.
 
Old 01-26-2012, 08:33 PM   #5
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 364

Rep: Reputation: 38
Quote:
Originally Posted by Dark_Helmet View Post
You can also try this script:
Code:
#!/bin/bash

find . -type f -mtime + 7 | \
while read filename ; do
  echo "rm \"${filename}\""
  #rm "${filename}"
done
I have not tested the above script. You need to do that yourself if you want to use it.
A simpler version of the script would be:

Code:
find . -type f -print0 -mtime +7 | xargs -0 rm -vf
If you have like a billion files, a simple one liner is

Code:
find . -type f -print0 -mtime +7 | xargs -0 -i rm -vf {}
But this would call rm for every file and so might take a while
 
1 members found this post helpful.
Old 01-26-2012, 08:57 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
The script was intended to display the actual rm command first--giving an opportunity to review what was going to happen before pushing the "big red button" and enabling the actual rm.

As for simplicity, I still like the one-liner at the beginning of my reply
 
Old 01-27-2012, 09:54 PM   #7
ankitpandey
Member
 
Registered: Jan 2012
Location: Mumbai
Posts: 63

Original Poster
Rep: Reputation: Disabled
Thanks for the help, it worked..
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script for Identifying the file and zip all files, move the files to Target Dir gvenkat Linux - Newbie 3 05-07-2011 10:53 AM
Count files in dir then move that many files in a script? chuckycharms Linux - Newbie 3 12-18-2009 11:31 AM
Creating a script to move or copy files into multiple directories below the files matthes138 Linux - Newbie 5 08-25-2009 04:57 PM
How can I move files from a host to another in a script eantoranz Linux - Networking 7 08-12-2005 02:39 PM
Script to Move files jain_rajesh Linux - Newbie 1 10-15-2004 08:53 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:46 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