I am looping over all the files in a directory with the following command:
for i in *.fas; do some_code; done;
However, I get them in this order
vvchr1.fas vvchr10.fas vvchr11.fas vvchr2.fas ...
instead of
vvchr1.fas vvchr2.fas vvchr3.fas ...
what is natural order.
I have tried sort command, but to no avail.
The syntax to loop through each file individually in a loop is: create a variable (f for file, for example). Then define the data set you want the variable to cycle through. In this case, cycle through all files in the current directory using the * wildcard character (the * wildcard matches everything).
readarray -d '' entries < <(printf '%s\0' *.fas | sort -zV) for entry in "${entries[@]}"; do # do something with $entry done
where printf '%s\0' *.fas
yields a NUL separated list of directory entries with the extension .fas
, and sort -zV
sorts them in natural order.
Note that you need GNU sort installed in order for this to work.
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