I want to delete folders and its files that created before than 7 days by command line , Thanks and regards

link|improve this question
On which OS (Win, OSx Linux? I am assuming Linux but you know what they say about assumptions. – Steve Robillard Dec 11 '11 at 5:09
sevenforums.com/tutorials/… – m.qayyum Dec 11 '11 at 5:11
Im not sure how you'd do the 7 day part, but if your using OS X or Linux you can use the command: rm -rf /path/to/the/folder – nosedive25 Dec 11 '11 at 5:12
wa c mahmoud. you want to create a script that delete files before the 7th day ? and in which operating system ?! – FGraviton Dec 11 '11 at 5:18
In windows, and the folders I want to be deleted named as: 200110001,20110002 .. and so on, just the folder start with "2011..." i want to delete – Mahmoud AL-saadi Dec 11 '11 at 5:21
show 1 more comment
feedback

migrated from stackoverflow.com Dec 11 '11 at 11:46

This question came from our site for professional and enthusiast programmers.

1 Answer

*NIX

If you are using *nix and have find available this should do the trick;

find /the/directory/containing/files/to/delete -mtime +7 -exec rm -r {} \;

The flag -mtime is to check the modification timestamp of the found files, if it's above 7*24h ago it will execute rm /path/to/file

From the manpage for find

-mtime n
    File's  data was last modified n*24 hours ago.  See the comments
    for -atime to understand how rounding affects the interpretation
    of file modification times.

WINDOWS

I never work on windows though I got curious to see what command to be the equivalent to the above in a MS-DOS environment, and I found Batch file to delete files older than N days here on stackoverflow.

The relevant command (copy+pasted from the previously linked thread):

forfiles -p "C:\what\ever" -s -m *.* -d <number of days> -c "cmd /c del @path"
link|improve this answer
Rm requires the -r flag to delete directories, if I remember correctly. (UNIX) – Jon Dec 11 '11 at 5:23
@Jon Oh, I didn't notice it was a requirement to delete both directories and files, I'll alter my post right away. Thanks for pointing this out! – refp Dec 11 '11 at 5:26
And just to be sure, it should also use the -f flag, so -rf will always force a delete. (forgot that one). – Jon Dec 11 '11 at 5:28
@Jon I left that out on purpose to be honest, better safe than sorry! ;) – refp Dec 11 '11 at 5:33
refp, thanks alot for your answer,"forfiles" will work in windows? – Mahmoud AL-saadi Dec 11 '11 at 5:47
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.