简体   繁体   中英

Python code to find all newly created, modified and deleted files in all the directories/sub-directories starting from / directory

I know how to list all subdirectories and files in a directory tree. But I am looking for way to list all newly created files, modified and (if possible) deleted files in all the directories in a directory tree starting from the root directory.

You could find all files created or modified in the last half-hour by looking at the "mtime" of each file:

import os
import datetime as dt

now = dt.datetime.now()
ago = now-dt.timedelta(minutes=30)

for root, dirs,files in os.walk('.'):  
    for fname in files:
        path = os.path.join(root, fname)
        st = os.stat(path)    
        mtime = dt.datetime.fromtimestamp(st.st_mtime)
        if mtime > ago:
            print('%s modified %s'%(path, mtime))

To generate a list of deleted files, you'd also have to have a list of files 30 minutes ago.


A more robust alternative is to use a revision control system like git . Making a commit of all the files in the directory is like making a snapshot. Then the command

git status -s

would list all files that have changed since the last commit. This will list files that have been deleted too.

from tempfile import mkstemp
import shutil
import os
import datetime as dt
import sys


# gets the time frame we are going to look back and builds a placeholder list to passover the info from our mtime to slay
now=dt.datetime.now()
ago=now-dt.timedelta(minutes=480)
passover=[]

# the '.' is the directory we want to look in leave it to '.' if you want to search the directory the file currently resides in
for root,dirs,files in os.walk('.'):
    for fname in files:
        path=os.path.join(root,fname)
        st=os.stat(path)
        mtime=dt.datetime.fromtimestamp(st.st_mtime)
        if mtime>ago:
            passover.append(path)


def slay(file_path, pattern, subst):
    #Create temp file
    fh, abs_path = mkstemp()
    with open(abs_path,'w') as new_file:
        with open(file_path) as old_file:
            for line in old_file:
                new_file.write(line.replace(pattern, subst))
    old_file.close()
    #Remove original file
    os.remove(file_path)
    #Move new file
    try:
        shutil.move(abs_path, file_path)
    except WindowsError:
        pass

#we pass the passover list to the slay command in a for loop in order to do muiltple replaces in those files.
for i in passover:
    slay(i,"String1","String2")

Take a look at "man find"

create a temp file to compare

example:

find / -type f -newerB tempFile

some part of the man find

-newerXY reference
          Compares  the  timestamp of the current file with reference.  The reference argument is normally the name of a file (and one
          of its timestamps is used for the comparison) but it may also be a string describing an absolute time.  X and Y  are  place‐
          holders for other letters, and these letters select which time belonging to how reference is used for the comparison.

          a   The access time of the file reference
          B   The birth time of the file reference
          c   The inode status change time of reference
          m   The modification time of the file reference
          t   reference is interpreted directly as a time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM