command line - Iterate over specific files in a directory using Bash find -
shellcheck doesn't like for
on find
loop in bash.
for f in $(find $src -maxdepth 1 -name '*.md'); wc -w < "$f" >> $path/tmp.txt; done
it suggests instead:
1 while ifs= read -r -d '' file 2 3 let count++ 4 echo "playing file no. $count" 5 play "$file" 6 done < <(find mydir -mtime -7 -name '*.mp3' -print0) 7 echo "played $count files"
i understand of it, things still unclear.
in line one: '' file
?
in line six: empty space in < < (find).
<
redirects, usual? if are, mean redirect do
block?
can parse out? right way iterate on files of kind in directory?
in line one: '' file?
according help read
, ''
argument -d
parameter:
-d delim continue until first character of delim read, rather newline
in line six: empty space in < < (find).
there 2 separate operators there. there <
, standard i/o redirection operator, followed <(...)
construct, bash-specific construct performs process substitution:
process substitution process substitution supported on systems support named pipes (fifos) or /dev/fd method of naming open files. takes form of <(list) or >(list). process list run input or output connected fifo or file in /dev/fd...
so is sending output of find
command do
loop.
are < redirects, usual? if are, mean redirect block?
redirect loop means command inside loop reads stdin
read redirected input source. side effect, inside loop runs in subshell, has implications respect variable scope: variables set inside loop won't visible outside loop.
can parse out? right way iterate on files of kind in directory?
for record, typically piping find
xargs
, although solution best depends extend on you're trying do. 2 examples in question different things, , it's not clear you're trying accomplish.
but example:
find $src -maxdepth 1 -name '*.md' -print0 | xargs -0 -idoc wc -w doc
this run wc
on *.md
files. -print0
find
(and -0
xargs
) permit command correctly handle filenames embedded whitespace (e.g., this file.md
). if know don't have of those, do:
find $src -maxdepth 1 -name '*.md' | xargs -idoc wc -w doc
Comments
Post a Comment