开发者

What's the best way to perform a differential between a list of directories?

I am interested in looking at a list of directories and comparing the previous list with a current list of directories and setting up a script to do so. Maybe in perl or as a shell script.

Should I use something like diff? Programatically, what would be an ideal way to do this? For example let say I output the diff to an开发者_如何学Python output file, if there is no diff then exit, if there is results, I want to see it.

Let's for example I have the following directories today:

/foo/bar/staging/abc
/foo/bar/staging/def
/foo/bar/staging/a1b2c3

Next day would look like this where a directory is either added, or renamed:

/foo/bar/staging/abc
/foo/bar/staging/def
/foo/bar/staging/ghi
/foo/bar/staging/a1b2c4


There might be better ways, but the way I typically do something like this is to run a find command in each directory root, and pipe the output to separate files. You can then diff the files using the diff tool of your choice. If you want to filter out certain directories or files, you can throw in some grep or grep -v commands in the pipeline, or you can experiment with options on the find command.

The other main option is to find a diff tool that offers directory/folder comparisons. Most of the goods ones support this, but I like the command line method, because you get more control over what you're diffing.

cd /my/directory/one
find . -print | sort > /temp/one.txt

cd /my/directory/two
find . -print | sort > /temp/two.txt

diff /temp/one.txt /temp/two.txt


also check the inotifywait command. it allows you to monitor files in RT.


You might also consider the find command using the -newer switch.

The usage is:

find . -newer timefile.txt -print

The -newer switch makes find return a list of files that are created or updated after the specified file's modification time. In the example above, any file created or updated after timefile.txt would be returned. You'd have to create a timefile.txt file, most likely once per day. Some versions of find have variations of newer that compare against other time stamps for a file (last modified, last accessed, last created, etc.)

This technique would not report a file that was deleted, however. A daily diff of the file listings could report that.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜