First of all, list all files older than 30 days under /opt/backup directory. Also, if required you can delete them with a single command. The strange thing about this is that you specify each command argument separately to find and terminate with a semicolon ( ). Using the find command, you can search for and delete all files that have been modified more than X days. To remove the files that match this criteria, you can use find's -exec command command. The following command lists all files which were modified more than 14 days a ago and accessed less than 60 seconds ago. Instead of using another process to find out if a file is currently "in use", you can check to see if was accessed within the last N seconds or days. These let you specify the file access time in days or seconds, respectively. To begin, lets see how to list files showing their date using the ls (List) command followed by the -l flag for long. Instead of using lsof which has a nightmare tangle of options and interesting ouput to parse, I suggest using the -atime or -amin options to find. Then to remove them all, assuming none of the file names contains a newline character, you can use f -mtime 14 -atime 14 -exec sh -c ' I recommend reviewing the files before deleting them. (Whether they do, and how often, depends on the kernel version, on the mount options, and on how the access time compares with the modification time.) I recommend at least testing that the file hasn't been read in a while this can be tested with the access time, but beware that Linux systems don't update the access time reliably. If ! fuser "$0" >/dev/null 2>/dev/null thenīeware that just because a file hasn't been modified in a long time and isn't currently open doesn't mean that it isn't useful. find /dir/* -type f -mtime 14 -exec sh -c ' To test whether a file is currently open, the most straightforward way is to call fuser. This executes a program with arguments if you need a more complex command (with variable expansion, conditionals, etc.) then you need to invoke a shell explicitly: find /dir/* -type f -mtime 14 -exec sh -c ' Use the -exec action in find to execute a command for each file.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |