I have a bash script that processes all of the files in a directory using a loop like
for i in *.txt
do
ops…..
done
There are thousands of files and they are always processed in alphanumerical order because of ‘*.txt’ expansion.
Is there a simple way to random the order and still insure that I process all of the files only once?
Assuming the filenames do not have spaces, just substitute the output of List::Util::shuffle.
If filenames do have spaces but don’t have embedded newlines or backslashes, read a line at a time.
To be completely safe in Bash, use NUL-terminated strings.
Not very efficient, but it is possible to do this in pure Bash if desired.
sort -Rdoes something like this, internally.Or use a traditional Fisher-Yates shuffle.