From owner-freebsd-hackers@FreeBSD.ORG Tue Nov 18 18:28:22 2003 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id B7C0D16A4CE for ; Tue, 18 Nov 2003 18:28:22 -0800 (PST) Received: from episec.com (episec.com [198.78.65.141]) by mx1.FreeBSD.org (Postfix) with SMTP id F298543FA3 for ; Tue, 18 Nov 2003 18:28:21 -0800 (PST) (envelope-from edelkind-freebsd-hackers@episec.com) Received: (qmail 15100 invoked by uid 1024); 19 Nov 2003 02:28:09 -0000 Date: Tue, 18 Nov 2003 21:28:09 -0500 From: ari To: freebsd-hackers@freebsd.org Message-ID: <20031119022809.GF58437@episec.com> Mail-Followup-To: ari , freebsd-hackers@freebsd.org References: <383537BA-1A23-11D8-B220-000A95775140@battleface.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <383537BA-1A23-11D8-B220-000A95775140@battleface.com> Subject: Re: mtime and directories... X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 19 Nov 2003 02:28:22 -0000 lists@battleface.com said this stuff: > Changing a file, of course, results in a change to its modification > time. Am I correct in determining that the mtime of the enclosing > directory is also updated? The mtime of a directory is updated when the directory "file" changes. Directory "file"s contain filenames and their associated inode numbers. Modify any of that information (e.g., create a new file, remove or rename an existing file), and the mtime will be changed. Changing the ctime or mtime of a file within a directory does not modify that of the directory itself. > The reason I'm asking this is because I need to be able to determine if > any file has changed within a directory from a shell script, Perl, or > Python. All I need to know is if at least one file has changed. I don't > need to find all such files. [Sidelight: anyone know how, if possible, > to stop find after finding one file? I can't figure this one out alone.] % find /some/dir |head -1 In this case, 'head' will exit after reading one line, causing 'find' to receive a SIGPIPE if it tries writing more data. The 'find' command doesn't typically trap SIGPIPE, so the process will exit. Of course, it won't receive the signal immediately --- only if it calls write(2) again, and on descriptor 1. If 'find' matches only one file, it will wind up seeking through the entire tree. This may not be sufficient for your application. To truly make find exit after it matches one file, you can do something along the lines of: % prescript find /some/dir -exec postscript '{}' \; ... where prescript contains a somewhat more error-tolerant version of the following: #!/bin/sh echo $$ >/pid/dir/find.pid exec $@ ... and postscript contains a somewhat more useful version of the following: #!/bin/sh dostuffwith $@ & pid=`cat /pid/dir/find.pid` rm -f /pid/dir/find.pid kill $pid > My initial solution was to use 'find' to find any files newer than a > marker file. However, I believe I've determined that the mtime of > enclosing directories also changes to reflect the last file that was > updated within its hierarchy. So, it makes sense that I simply check > the directory that contains the marker file. Again, a directory's modification time is updated only if the actual directory contents have changed, not the contents or inodes of the files within it. ari