简体   繁体   中英

Wildcards in KornShell (ksh)

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM