2

wget's timestamping functionality ensures it only downloads files that have changed. I'm looking for a linux command-line tool that will recursively compare a directories contents with a saved version (from the last time it ran), and execute an arbitary shell command on any new or updated files. Basically:

run-on-updated --directory /path/to/my/dir --last-results ~/.lastrun --exec "echo {}"

I could even imagine this as an extension of the 'find' command. Does such a tool exist?

2
  • ~/.lastrun is just a file, right? Not a copy of the complete directory? In the latter case you could start with rsync -v --dry-run /path/to/my/dir ~/.lastrun
    – mpy
    Commented Mar 2, 2013 at 23:21
  • Ideally it should be just the file metadata (e.g. path, size, last-modified) rather than a copy of the directory as that'd be too large. rsync definitely has this timestamping functionality though - but I hope to find it as a standalone tool (the unix philosophy?) Commented Mar 2, 2013 at 23:33

2 Answers 2

3

An attempt to answer the question -- probably with lots of room for improvement:

tar -cv -f /dev/null -g ~/.lastrun /path/to/my/dir | grep -ve '/$' | xargs -I{} echo '{}'

Note for the tar command: "The option ‘-g’ instructs tar to operate on an incremental archive with additional metadata stored in a standalone file, called a snapshot file. The purpose of this file is to help determine which files have been changed, added or deleted since the last backup, so that the next incremental backup will contain only modified files." (Quote from doc)

The grep command excludes directory names (ending with /) which tar always outputs, and finally xargswill run the desired command on the files.

2
  • 1
    This works! It updates the ~/.lastrun file in-place which is useful too. Commented Mar 3, 2013 at 1:29
  • Glad it works for you. Please take care if there are some file names with whitespaces (I was too tired yesterday to think of that ;) You'll be saver when using xargs -I{} echo '{}' -- I edited my answer accordingly.
    – mpy
    Commented Mar 3, 2013 at 10:55
1

I am not sure what you mean by the sentence "compare a directories contents with a saved version", but to do what the answer of mpy does, I would prefer find:

Before running the find command the first time, you need to run the touch command do create the timestamp file:

touch .lastrun

Later, run the touch command after the find run:

find my/dir -type f -newer .lastrun -exec echo '{}' \;
touch .lastrun

The file .lastrun will never be listed itself, because it is never newer than itself.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .