How to find and delete files older than X days in Linux?

How to find and delete files older than X days in Linux?

Find and Delete Files Older Than X Days In Linux. First, let us find out the files older than X days, for example 30 days. To do, so, just run: $ find . -mtime +30 -print. The above command will find and display the older files which are older than 30 day in the current working directorys. Here,

How to list only files in current directory?

I want to list only files in current directory which are older than x days. I have below restriction Output should look like file1 file2 file3 .. I used find . -mtime +30. but this gives files and files in sub-directories as well. I would like to restrict doing search recursively and not to search inside directories. Thanks a lot in advance !

Is there a command to find older files?

Not directly related to question, but might be interesting for some that stumble here. find command doesn’t directly support -older parameter for finding files older than some required date, but you can use negate statement (using accepted answer example): will return files older than provided date.

How to get rid of old files in find?

Let’s say you want to remove all those old files: If you are piping the output of find into another program and there is the faintest possibility that the files which you are searching for might contain a newline, then you should seriously consider using the -print0 option instead of -print.

How to find all files older than 15 days?

You could start by saying find /var/dtpdev/tmp/ -type f -mtime +15. This will find all files older than 15 days and print their names. Optionally, you can specify -print at the end of the command, but that is the default action. It is advisable to run the above command first, to see what files are selected.

What’s the best way to remove files older than 30 days?

I am working on a C# Windows application, .NET 3.5. My app generates tons of log files which has a current date put in the file name like so 20091112. What would be the best strategy to remove files older than say 30 days.

How to delete files older than n days in Java?

Recursive option, that traverses sub-folders and deletes all files that are older than N days: