LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Script to move/delete aged files (https://www.linuxquestions.org/questions/linux-general-1/script-to-move-delete-aged-files-924788/)

ankitpandey 01-20-2012 02:59 AM

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

Dark_Helmet 01-23-2012 12:54 AM

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).

ankitpandey 01-23-2012 01:22 AM

Below is the command which i am using

find . -type f -mtime +7 | xargs rm

Dark_Helmet 01-23-2012 01:32 AM

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.

the_gripmaster 01-26-2012 08:33 PM

Quote:

Originally Posted by Dark_Helmet (Post 4581829)
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

Dark_Helmet 01-26-2012 08:57 PM

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 ;)

ankitpandey 01-27-2012 09:54 PM

:cool: Thanks for the help, it worked.. :)


All times are GMT -5. The time now is 10:55 AM.