How do I delete folders and files that were older than 7 days on the command line?
I want to delete folders and its files were created longer than 7 days using the command line.
*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"