1836201 Members
2928 Online
110096 Solutions
New Discussion

Remove files

 
zsujith
Frequent Advisor

Remove files

Hi,

i have 5000 files in one directory. I want to remove files except first 8 days files.

Waiting for your response.

Thanks & Regards
"The most wasted day is that in which we have not laughed."
6 REPLIES 6
Davis Paul
Valued Contributor

Re: Remove files

Hi Zsujith,
Try the following steps:
1) Create a new temporary directory say /var/new
#mkdir /var/new
2) move to the particular directory in which u want to remove the files.
# cd â dirnameâ
3) find the file which are 8 days old and copy to /var/new
#find . -xdev -type f -mtime -8 -exec cp /var/new
4) Delete the files in your dir
#rm â rf *
5) Copy back your 8 days old files to your directory.
#cp /var/new/* .

Regards
Davis Paul.

Pete Randall
Outstanding Contributor

Re: Remove files

Well, if you truly mean just the ones newer than those created in the first 8 days, you'll want to create a reference file with a time stamp equal to your cut off date using the touch command, then use find with the -newer switch to move all the files elsewhere.


Pete

Pete
V. Nyga
Honored Contributor

Re: Remove files

Hi,

check 'man find' - there you can see the options you can use, for example:
'find . -type f ! -mtime -365 -exec ll {} \;'
'find . -type f ! -mtime -365 -exec rm {} \;'
(this is for files older than 1 year - it's always good to check a syntax with a 'll' at the end, so you can check if it works like you want).

You have to decide if you want to delete files older or newer than a defined date.

HTH
Volkmar
*** Say 'Thanks' with Kudos ***
Davis Paul
Valued Contributor

Re: Remove files

Sorry , my third step(exec) should finished with {} \;
James R. Ferguson
Acclaimed Contributor

Re: Remove files

Hi :

Instead of terminating the '-exec rm {}' with a semicolon, use a "+" character. The use of the semicolon means that ONE process will be spawaned for every argument. With the "+" character, multiple arguments will be passed to a very few (or even one) process.

Thus:

# find /tmp -type f -mtime +8 -exec rm {} \+

...is far, far faster than:

# find /tmp -type f -mtime +8 -exec rm {} \;

Regards!

...JRF...
Arturo Galbiati
Esteemed Contributor

Re: Remove files

Hi,
find . -type f !_newer |xarsg rm {} +

man find for more detail about newer option.
HTH,
Art