The Computer Oracle

Watch Filesystem in Real Time on OS X and Ubuntu

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Hypnotic Orient Looping

--

Chapters
00:00 Watch Filesystem In Real Time On Os X And Ubuntu
00:39 Accepted Answer Score 10
02:55 Accepted Answer Score 12
03:44 Answer 3 Score 2
05:13 Answer 4 Score 2
05:40 Answer 5 Score 0
05:59 Thank you

--

Full question
https://superuser.com/questions/431624/w...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#linux #macos #sync #inotify

#avk47



ACCEPTED ANSWER

Score 10


Probably the often cited relation of the origin of the expression piece of cake with cakewalk is only incidental. As suggested in the following extract from The Grammerphobia the idea of cake as something pleasant and easy (to swallow) dates back at least to the 16th century. Piece of cake may be just a later version along those lines:

The Oxford English Dictionary doesn’t comment on the difficulties of cake-making, but it agrees with you that the colloquial phrase “a piece of cake” refers to “something easy or pleasant.”

How did cake get this reputation?

As the OED explains, cake is associated figuratively, especially by children, “as a ‘good thing,’ the dainty, delicacy, or ‘sweets’ of a repast.”

Cake comes off as highly rated in other phrases as well.

The expression “you can’t have your cake [that is, keep your cake] and eat it too” dates back, in various forms, to the 1500s.

  • Here’s its earliest incarnation, from John Heywood’s Proverbs and Epigrams (1562): “Wolde ye bothe eate your cake, and haue your cake?”

The phrases “cakes and ale” (in England) and “cake and cheese” (in Scotland) have been used since the early 1600s as metaphors for the good things in life.

Similarly, the 19th-century American expression “*to take the cake” means “to carry off the honours, rank first,” the OED says, adding that it’s “often used ironically or as an expression of surprise.”

And of course, any extra trimmings in the way of good luck will inevitably be described as “the icing on the cake” (1969).

“a piece of cake.” The OED’s first citation comes from a collection of light verse by Ogden Nash, The Primrose Path (1935): “Her picture’s in the papers now, / And life’s a piece of cake.”

The AHD has a different suggestion, as you noted, as to its origin, but both sources see its earliest usages from the ‘30s. I’d point out that the supporting idea is the same:

Possibly it evokes the easy accomplishment of swallowing a slice of sweet dessert.

2) Is there an earlier citation than 1936?

Note that the Primrose Path was published in 1935, not in 1936 as a number of sites suggest: enter image description here

3) Is the idiom “piece of cake” American or British?

The sites The Babbel Blog, Smartling and mainly Not one-off Britishism classify “a piece of cake” as an AmE idiom.

“a piece of cake” is as American as red velvet cake.”




ACCEPTED ANSWER

Score 12


OS X would typically use Folder Actions or Launchd for this task.

The only cross-platform tool I know is the watchdog API for Python (thus available for OS X and Linux). Install it through pip (or easy_install)

pip install watchdog

It comes with the watchmedo command line tool, which allows you to run shell commands on system events. Check its general syntax with watchmedo --help

Here is a short example, where you could easily modify the command and the path highlighted in bold:

watchmedo shell-command \
    --recursive \
    --command='echo "${watch_src_path}"' \
    /some/folder

This would simply spit out the paths to all changed files or folders, allowing you to pipe watchmedo's output to another shell command.




ANSWER 3

Score 2


I've tweaked some Ruby scripts I've come across to do exactly what you're looking for. Here's the Ruby code:

#!/usr/bin/ruby
# Inspired by http://vikhyat.net/code/snippets/#watchfile

# How to use:
# This script takes two paramaters:  a folder and a shell command
# The script watches for when files in the folder are changed.  When they are, the shell command executes
# Here are some shortcuts you can put in the shell script:
# %F = filename (with extension)
# %B = base filename (without extension)


unless ARGV.length == 2
  puts "\e[32m    Usage: \e[0mruby OnSaveFolder.rb 'folder' 'command'"
  exit
end

require 'digest/md5'
require 'ftools'

$md5 = ''
$directory = File.expand_path(ARGV[0])
$contents = `ls #{$directory}`
$contentsList = $contents.split("\n")
$fileList = []


Dir.chdir($directory)

for i in $contentsList
  if ( File.file?(File.expand_path(i)) == true)
    $fileList.push(i)
  end
end

$processes = []

def watch(file, timeout, &cb)
  $md5 = Digest::MD5.hexdigest(File.read(file))
  loop do
    sleep timeout
    if ( temp = Digest::MD5.hexdigest(File.read(file)) ) != $md5
      $md5 = temp
      cb.call(file)
    end
  end
end

puts "\e[31m Watching files in folder \e[34m #{$directory}"
puts "\e[32m    Press Control+C to stop \e[0m"

for i in $fileList
  pid = fork do
    $num = 0
    $filePath = File.expand_path(i)

    watch i, 1 do |file|
      puts "\n     #{i} saved"

      $command = ARGV[1].dup
      if ( ARGV[1].include?('%F') )
        $command = $command.gsub!('%F', i)
      end
      if ( ARGV[1].include?('%B') )
        $command = $command.gsub!('%B', File.basename(i, '.*'))
      end

      $output = `#{$command}`
      if ( $output != '')
        puts "\e[34m     #{$command}\e[31m output: \e[0m"
        puts $output
      end
      puts "\e[34m     #{$command}\e[31m finished \e[0m (#{$num}, #{Time.now})\n"
      $num += 1

    end
  end
  $processes.push(pid)
end

Process.wait(pid)

for i in $processes
  `kill #{i}`
end

I named this script "OnSaveFolder.rb". It takes two parameters: the folder you want to watch for changes, and the bash code you want to run when there's been a change. For example,

ruby OnSaveFolder.rb '/home/Movies' 'echo "A movie has been changed!"'

I hope this helps! I've found that ruby works very well for this type of thing, and it's installed on OS X by default.




ANSWER 4

Score 2


You could put lsof into a watch command and have it update every <s> seconds:

watch -n <s> lsof

and launch that with whatever filter, such as watching pid1, pid2, pid3 and ignoring pid4

lsof -p "pid1,pid2,pid3,^pid4"

If that's not enough, you could always write your own filters with grep.

For OS X, see this question's answers.




ANSWER 5

Score 0


entr is nice. You can filter the exact files you want it to watch, like this.

find . -name '*.py' | entr python runTests.py

If you want it to only monitor certain files inside a directory, you can craft your find command so that it only returns the files you want to watch.

I find it easier to configure than fswatch.