The main problem with the backtick invocation in the question is that first the find command lists all files under /tmp, and then passes that (huge) list as arguments to touch
, which is too much for a single invocation. There is also a problem if filenames contain spaces, newlines, semicolons, ampersands, pipe chars or similar.
Better to use this construct:
find /tmp -type f -exec touch -c '{}' +
The find
command recursively lists all files (directories, symlinks, device specials are also "files" in unix) under /tmp
.
-type f
tells find to only select regular files
The -c
option to touch
prevents touch from creating files that do not exist.
It seems you omitted some logic in your example, but if you want to do it only 200 times, you could try the following:
#!/bin/bash
dayNo=1
while test $dayNo -le 200
do
find /tmp -type f -exec touch -c '{}' +
sleep 86000
dayNo=$(expr 1 + ${dayNo})
done
dayNo
. – Dubu Aug 20 '14 at 06:27-c
option to touch, this should avoid a race condition if the file is removed prior to touching it, preventingtouch
from re-creating it. – MattBianco Aug 20 '14 at 06:54