Reputation: 1623
When I am in my home directory and type “ls *s*” in the terminal, it shows me all folders and files that have ‘s’ in their names (Music for example). But when I type “ls *si*”, it does not show anything (I think Music should be listed). Why is that?
Upvotes: 1
Views: 1917
Reputation: 222886
This is an illusion. The wildcards are expanded before the command is executed, and what “ls” displays depends on how many words result from the expansion. When “ls” lists multiple things, it shows the name of each folder it lists. When “ls” lists just a single folder, it shows only the contents, without the name.
When you type “ls *s*”, the string expands to more than one name, because there is more than one name in your directory containing “s”. The result is as if you had typed something like “ls Desktop Music”. When “ls” lists multiple things, it shows the directory name as well as the contents of each directory, so you get a listing such as:
Desktop:
foo
Music:
iTunes
When you type “ls *si*”, the string expands to only one name, because the only name in your directory containing “si” is “Music”. The result is as if you had typed “ls Music”. When “ls” lists one folder, it shows only the contents of the folder, without the name, so you get a listing such as:
iTunes
To make “ls” list only the things that match, not their contents, use “ls -d *si*”. “-d” says to list directories the same way it lists files, not to list their contents.
Upvotes: 5