Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

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.)

share|improve this answer

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.

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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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