Bash – Watch command with ls and shell globs

bashwatchwildcards

I tried running the command

watch -n1 ls foo/bar*

which I want to list files matching foo/bar* every second. However, what actually happens is that the shell expands the glob and what watch runs is, say,

ls foo/bar1.txt foo/bar2.txt

Thus, if another file foo/bar3.txt is added later it won't show up in the output. Is there a neat way to do this without creating a script file containing

ls foo/bar*

Best Answer

Wrap it in strong quotes so that the wildcard isn't parsed immediately:

$ watch -n1 'ls foo/bar*'