I am interested in this command:

ls [A-D]?.txt

This is a Unix question on an assessment. We are asked to define what this command does.

  • 1
    Try it yourself? Create a number of files in a directory on a system, and see what displays when you enter the command. You should be able to guess what it'll do, but this trial will clarify the results for you. – guiverc Jul 27 '19 at 11:27

Simply describing what the command does would likely be useless to you, in part because that specific command is probably not itself of much interest, but also because its effect is somewhat context-dependent (see below). What is of greater interest--and what I hope may more usefully answer the question for you and for others who find this page by searching--is information about the concepts involved and what documentation to consult.

Most shells perform globbing, which is also called filename expansion and pathname expansion. In globbing, text that contains particular special characters is treated as a pattern and expanded into the names of existing files that match the pattern. Different shells differ in their syntax but the common syntax--which is standardized by POSIX--treats *, ?, and [ specially.

For general information on globs you can run man 7 glob. This shows a manual page that explains the common syntax and also covers some important conceptual issues, including how globs are not the same as regular expressions, even though they have some similarities.

Then, onto the specifics: globbing is performed by your shell, so consult your shell's documentation. If your shell is bash, you could run man bash, but the best documentation to consult is the Bash reference manual. In it you can verify that one of the expansions performed by the shell is filename expansion, and read the details of how filename expansion is performed.

I hope you don't think I'm trying to withhold information, as that is not my goal. I simply don't have the context you have about what material your course has covered and what assumptions and level of detail is desired. There are some intricacies involved here. The reference manual describes the shell options and environment variables that affect the result.

Assuming your shell is bash, I highly recommend reading both the Filename Expansion and Pattern Matching sections, and remember that the current locale may affect what patterns like [A-D] match.

Depending on the nature of your course and where you are in it, this may be more information than you need. But becoming familiar with the documentation will make things easier and gives you the power to efficiently figure out how to do what you need to do. In any case, you can consult your instructor and/or course materials if you're not sure what's relevant or important for the purposes of your assignments.

As for ls, that's an external command you can learn about how it works by running man ls or consulting the GNU coreutils reference manual.

Manual pages, which you can read with the man command, can also be browsed and read online. One of the places you can do this is the Ubuntu Manpage Repository.

Finally, you may be interested to look at the POSIX standards, if not now than at some point. The part about the Shell Command Language covers the POSIX shell and includes material on filename expansion.

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