i have a piece of code that should print all files in specific dir. i use find exec for this:
find ${_di} -type f -print -exec Log "$(stat -c%y {}) - {}" \;
Where log is function of mine defines in same file. But id does not work and i get error message:
"find: Log: No such file or directory".
Why? What is wrong in this piece of code?
Function can't be used in -exec however bash -c can be used as command.
Slightly modified to using + as -exec command terminator and {} last to allow to reduce the number of bash processes spawned.
find ${_di} -type f -print -exec bash -c "$(typeset -f Log)"$'\n''for arg; do Log "$(stat -c%y "$arg") - $arg"; done' -- {} +
the argument -- can be replaced by anything else it is used for $0 argument of shell.
bash -c 'echo $0' hello
Maybe -printf "%TY-%Tm-%Td %TT - %p\n" option could achieve the same result, more efficiently without launching other process.
Also using echo may be less safe than using find -print option, considering the following use case.
touch file.$'\e#8'
find . -type d ! -name . -prune -o -name file'*' -print
find . -type d ! -name . -prune -o -name file'*' -exec echo {} \;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With