Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to delete folders and its files were created longer than 7 days using the command line.

share|improve this question

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

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

    
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

2 Answers 2

*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 XP & VISTA

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

WINDOWS 7

Syntax has changed a little therefore the updated command is

forfiles -p "C:\what\ever" -s -m *.* /D -<number of days> /C "cmd /c del @path"
share|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

If you need to deal with space limit issue on a very large file tree (in my case many perforce branches), that sometimes being hanged while running the find and delete process -

Here's a script that I schedule daily to find all directories with specific file ("ChangesLog.txt"), and then Sort all directories found that are older than 2 days, and Remove the first matched directory (each schedule there could be a new match):

bash -c "echo @echo Creating Cleanup_Branch.cmd on %COMPUTERNAME% - %~dp0 > Cleanup_Branch.cmd"
bash -c "echo -n 'bash -c \"find ' >> Cleanup_Branch.cmd"
rm -f dirToDelete.txt
rem cd. > dirToDelete.txt 
bash -c "find .. -maxdepth 9 -regex ".+ChangesLog.txt" -exec echo {} >> dirToDelete.txt \; & pid=$!; sleep 100; kill $pid "
sed -e 's/\(.*\)\/.*/\1/' -e 's/^./"&/;s/.$/&" /' dirToDelete.txt | tr '\n' ' ' >> Cleanup_Branch.cmd
bash -c "echo -n '-maxdepth 0 -type d -mtime +2 | xargs -r ls -trd | head -n1 | xargs -t rm -Rf' >> Cleanup_Branch.cmd"
bash -c 'echo -n \" >> Cleanup_Branch.cmd'
call Cleanup_Branch.cmd

Note the requirements:

  1. Deleting only those directories with "ChangesLog.txt", since other old directories should not be deleted.
  2. Calling the OS commands in cygwin directly, since otherwise it used Windows default commands.
  3. Collecting the directories to delete into external text file, in order to save find results, since sometimes the find process has hanged.
  4. Setting a timeout to the find process by using & background process that being killed after 100 seconds.
  5. Sorting the directories oldest first, for the delete priority.
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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