To move files older than 31 days to a different folder.

To move files older than 31 days to a different folder.

find . -mtime +31 -exec mv {} Apr08-Oct08/ \;

' .' is current folder

Advertisements

How to execute commands on the files you find with ‘find’

How to execute commands on the files you find with ‘find’

If you want to run a command on every file found with the ‘find’
command, you can use the ‘-exec’ command line option:

# find . -mtime +10 -exec rm -r {} \;

The find command is normal, and we won’t be explaining it here. Suffice
it to say that this command will find all files under the current
working directory that are 10 days old or older, and delete them.

    * The ‘-exec’ option tells find to execute the command that follows on
      every file found.
    * The ‘rm -r’ is the command that will be executed.
    * The ‘{}’ (curly braces) in the find command are special. They are
      replaced, at run-time, with the currently “found” file.
    * The ‘\;’ is just the end-of-line marker for the find command. This is
      necessary because you could easily tell find to execute a new find
      within the ‘-exec’ part of the command line. When you nest commands
      like this, you need some way to identify the end of a particular
      command. The ‘;’ does this. The ‘\’ is to escape the ‘;’, because
      the ‘;’ has special meaning to the shell, as well as to the find
      command.

‘find’ing files older than some date

‘find’ing files older than some date

I just wanted to capture this real quick. I’m sure most UNIX admin types have this cemented in their mind somewhere, but I always forget it:

# find . -ctime +1 -name "*.dbf" -print

I used the above command to find all .dbf ( -name “*.dbf” ) files in the current directory ( . ) that had their status changed ( file attributes ) more than one day ago ( -ctime +1 ). This also works to search for files that have been accessed more than one day ago ( -atime +1 ), or to check that the contents were changed more than one day ago ( -mtime +1 ).

You can also specify less than one day ago ( -ctime -1 ), or exactly one day ago ( -ctime 1 ).

So, the format for the time options is:

    * -mtime n
    * -ctime n
    * -atime n

where n is a number of days. The following applies:

    * A number with no sign, for example, 3 (as in -mtime 3 or -atime 3), means the 24-hour period that ended exactly three days ago (in other words, between 96 and 72 hours ago).
    * A number with a minus sign (-) refers to the period since that time. For example, -3 (as in -mtime -3) is any time between now and three days ago (in other words, between 0 and 72 hours ago).
    * Naturally, a number with a plus sign (+) refers to the 24-hour period before that time. For example, +3 (as in -mtime +3) is any time more than three days ago (in other words, more than 96 hours ago).