Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I want to concatenate a bunch of files together in filename order.

Is it safe to assume that this will give me them in alphanumeric order?

cat * 

i.e. the same order that ls gives.

share|improve this question
add comment

2 Answers

up vote 10 down vote accepted

Short answer: Yes it will.

From the bash man page:

After word splitting, unless the -f option has been set (see The Set Builtin), Bash scans each word for the characters ‘*’, ‘?’, and ‘[’. If one of these characters appears, then the word is regarded as a pattern, and replaced with an alphabetically sorted list of file names matching the pattern.

share|improve this answer
4  
Note that "alphabetic" varies depending on locale settings (in particular $LC_COLLATE). –  grawity Sep 24 '10 at 12:43
2  
@grawity true, yet it would result in the same output as ls on the same system would. –  BloodPhilia Sep 24 '10 at 13:21
add comment

The short answer is yes, but you need to be aware of what it thinks the alphanumeric order should be, as it may not correspond to the natural order you have in mind.

For example, run some test on how upper and lower case are handled to find out on your system, as well as sequences of numbers.

A common problem I have run into in ordering lists of files returned by a glob is numbered files. I am often given files numbered like:

blah_1.txt
blah_2.txt
...
blah_10.txt
blah_11.txt

However, a glob using the * will not return them in this order, instead you will get something like:

blah_1.txt
blah_10.txt
blah_11.txt
...
blah_2.txt
blah_20.txt
...

So - to avoid this problem you'll either need to do some manipulation on the returned list of files, or rename files so that single digits have leading zero (i.e. blah_01.txt, blah_02.txt, etc.).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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