11

On my user folder, I have folders like D ropbox, D ocuments, D ownloads & d ump.

ls a* should list all the files whose name starts with a. Right? But when I gives the command ls d*, instead of just displaying dump it shows me the ls of dump directory. And When I gives the command as ls D*, then it displays ls of all foldes starting with D

So what part did I miss in understand it?

Edit: Attaching a screenshot ls screenshot

3 Answers 3

18

According to bash man page:

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

and from info node for ls

The `ls' program lists information about files (of any type, including directories). Options and file arguments can be intermixed arbitrarily, as usual.

For non-option command-line arguments that are directories, by default 'ls' lists the contents of directories, not recursively, and omitting files with names beginning with '.'. For other non-option arguments, by default `ls' lists just the file name. If no non-option arguments are specified, 'ls' lists the contents of the current directory.

so when yo give ls d*, d* is expanded to sorted list of all file/directory names starting with d. So you command becomes

ls dump

and as dump is a directory name so you get the listing of all files in the directory "dump' but when you give ls D*, D* is expanded to "Desktop Documents Downloads Dropbox", so the command becomes

ls Desktop Documents Downloads Dropbox

and as all of these are directories, you get the listing of files in individual directory. If there was a file starting with D in current directory it would have been listed seperately.

2
  • 2
    A minor addition: When more than one directory is given as an argument, ls displays the name of each directory before listing its content. It will not do it for a single directory. Feb 10, 2011 at 16:17
  • Thanks! That cleared my doubt. I did a ls p* and it showed up a file because it exists and a ls P* shows the Pictures Public folder as there is no file starting with P. And when I created a directory named poo, it showed up on ls p*
    – Ashfame
    Feb 10, 2011 at 16:29
7

ls d* and D* both list files and folders matching d* and D* respectively, the behaviour is the same, there must have been a mistake on your test.

If you want to list only the folder names and not the folder contents you need to use "-d":

ls -d D*
1
  • I have attached a screenshot, please take a look
    – Ashfame
    Feb 10, 2011 at 16:00
0

As suggested above, -d option is way to go. Adding

alias ld='ls -d'

to my ~/.bashrc file works well for me.

1
  • 3
    With this alias, you block easy access to ld - The GNU linker, which will keep the OP from compiling and linking programs. Use alias lsd='ls -d' instead. Before aliasing something as foo always do type foo to see if foo is already used as the name for something, and decide if the convenience of the alias override the loss of access to the previous foo
    – waltinator
    Jan 13, 2016 at 15:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .