I am trying to write a simple back up script in python where I try to list the files that are 24 hours old in specific directories that I would choose.

I read the manual of find and used

find . -mtime 1 > log.dat

to get the list of files in the log.dat however I also get the path information in that list as such

./hpc06MatlabCodes/2011/Apr/3dBoxModel
./hpc06MatlabCodes/2011/Apr/3dBoxModel/vfluidIrca10.dat ./hpc06MatlabCodes/2011/Apr/3dBoxModel/vLRecoveredSystem.mat

is there a way to exclude the directories and only get the files list. Greetz, Umut

link|improve this question
feedback

2 Answers

up vote 1 down vote accepted

Add the '-type f' flag to find:

$ find . -type f -a -mtime 1 > log.dat

(the -a is 'and' - it's the default conjunction but I like to specify it in case the default changes at some point in the future.)

link|improve this answer
feedback

Since you are doing this is python, I would use:

def get_old_files(topdir, howold=24*3600):
    import os, time
    now = time.time()
    filelist = []
    def traverse_links(filename):
        if not os.path.islink(filename):
            return filename
        return traverse_links(os.path.normpath(
                    os.path.join(os.path.dirname(filename), os.readlink(filename)))))
    for dirpath, dirnames, filenames in os.walk(topdir):
        for name in [traverse_links(os.path.join(dirpath, f)) for f in filenames]:
            try:
                if os.path.isfile(name) and now - os.path.getmtime(name) > howold:
                    filelist.append(name)
            except OSError:
                pass # ignore bad symlinks
    return filelist

This should do what you wish, you could also add an optional argument that will curtail directories. The advantage of using this over calling find is the extra overhead involved with spawning a new process as opposed to doing it all inside the process.

link|improve this answer
Thx for the script – Umut Tabak Apr 9 '11 at 19:19
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.