Home > Uncategorized > Delete old files in Linux

Delete old files in Linux

December 31st, 2008 nawaman Leave a comment Go to comments

With the power of command-line, you can process files that are fitting to difference criteria with in one or a few command. Of course, a regular one file deleting can be done with GUI like Nautilus so it is not a case that you MUST use command line a not GUI.

The power tool we are talking about is find. The command find is used to find files that fit certain criteria. It allows the search for files with so many criteria such as name, created/modified date, user/groups and many more. Consult its man page here for more information.

Today example is to delete old files. So let say you want to remove files that have not been modified longer than 24 hours. Here is the command

find /path/to/file -mtime +1 -exec rm {} \;

-mtime specified that you want to select files in the path that have not been modified since 24 hour ago (+1 means 1 time of 24 hours). You can use +7 to specified one week or older.
You can use -mmin to specified that in minutes.
-exec tells find what do you want to do with the matched files. In this case, we want to remove them that’s why we use rm.
NOTE: Notice a space before ‘\;’. It is needed.

If you want to remove the files based on the time it is created, used -ctime or cmin (day or minute). And use -atime or -amin the the time it is last accessed.

You can also added other criteria such as file name (-name “…”) or what ever valid find criteria.

So now you can delete old files or copy them to other location for backup using just one command.

NOTE: If you want to only get the list of the files and with GUI, use the program called gnome-search-tools. Gnome-search-tools was actually the default GUI for file searching of Gnome until it is replace by tracker search tool. However, I found it to be more useful and even more useful when you don’t tracker. Ubuntu 8.10 still have Gnome-search-tools it just that it is not in the menu. You can add it by yourself using the menu editor (right click on the main menu).

NawaMan

Categories: Uncategorized Tags:
  1. No comments yet.
  1. No trackbacks yet.