Reading between the lines of your question, I suspect what you are really asking is "how to prevent xargs from running when its standard input is empty" so that ls
is never run with no argument (which would list files in the current directory).
GNU xargs
has an option for that; from man xargs
:
-r, --no-run-if-empty
If the standard input does not contain any nonblanks, do not run
the command. Normally, the command is run once even if there is
no input. This option is a GNU extension.
However GNU find
also provides a mechanism to avoid piping its output to xargs
in most cases - from man find
:
-exec command {} +
This variant of the -exec action runs the specified command on
the selected files, but the command line is built by appending
each selected file name at the end; the total number of invoca‐
tions of the command will be much less than the number of
matched files. The command line is built in much the same way
that xargs builds its command lines.
So for example you could do
Vformat=(mp4 wmv pgep mov)
for File in "${Vformat[@]}"
do
find "$SrcDir" -iname "*.$File" -exec ls -l --time-style=full-iso {} +
done
If you want to avoid the overhead of calling find
multiple times, then one way to do that would be to convert your array of extensions into an array of -iname xxx
arguments connected with the -o
logical OR conjunction:
Vformat=(mp4 wmv pgep mov)
args=()
for File in "${Vformat[@]}"
do
args+=( -o -iname "*.$File" )
done
and then
find "$SrcDir" -type f \( "${args[@]:1}" \) -exec ls -l --time-style=full-iso {} +
The array slice ${args[@]:1}
removes a leading -o
and the literal brackets \(
and \)
group the OR list since -exec
has AND precedence.
Finally, if you just need file names and mtimes then you might consider using the find
command's -printf
instead of the external ls
:
find "$SrcDir" -type f \( "${args[@]:1}" \) -printf '%TF %TT %Tz\t%p\n'