I have two identical folders with files, one on external exfat drive, the other one on local drive. When I grep -r folder on external drive it searches files in alphabetical order, yet if I do the same for local folder the search order seems random. Why is this happening and how to use alphabetical order on local drive too?


It depends on file systems. Some file systems may have files "naturally" ordered and some have not (and in some cases ordering could be just a coincidence). Tried with 3 different file system (ext4, tmpfs, and vfat), and creating files named 3, 1, 2 in that order in a new directory yielded 3 different orders from grep -r ., none being the "natural" one.

When you use grep -r the file name if part of the output anyway so you can always sort the output (and perhaps cut the filename part in a later pipe operation).

Otherwise, AFAIK, ls and bash wildcards always yield files sorted according to the locale, so instead of grep -r try something like grep */*.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.