6

On Centos and Debian I tried this.

Does the * with the ls command imply recursive search?

If I start to type a dir name and hit tab I see this:

# ls /etc/rc<tab>
rc0.d/    rc1.d/    rc2.d/    rc3.d/    rc4.d/    rc5.d/    rc6.d/    rc.local  rcS.d/

I thought if I put an asterisk in the path I would see the same thing but instead it did a recursive search:

# ls /etc/rc*
/etc/rc.local

/etc/rc0.d:
K50netconsole  K90network

/etc/rc1.d:
K50netconsole  K90network

/etc/rc2.d:
K50netconsole  S10network

/etc/rc3.d:
K50netconsole  S10network
...

The windows dir command would just show me the directories matching /etc/rc* how can I get this behavior from ls (without using the tab key)?

****Edit/Update:

OK so now I understand * is interpreted by bash instead of the command, but is it always interpreted as "enumerate file and folder entries" or if I use it in a different context does it do something else and if so is there like some big if statement in bash that says "if * used with file system paths return file and folder names, if * used with rpm (like rpm -qa post*) return package names, etc"

1
  • 3
    About the last question: try touch postxpto followed by rpm -qa post*. Feb 14, 2016 at 20:31

2 Answers 2

8

Asterisk expands before it gets to the ls command, so this is an equivalent of

ls /etc/rc0.d    /etc/rc1.d   /etc/rc2.d    /etc/rc3.d    /etc/rc4.d ...

which will list contents each directory separately.

You might want to use ls -d /etc/rc* to suppress listing the files inside the directories, or use a command that just displays the expansion, such as echo /etc/rc*.

2
  • 1
    So then * isn't just a wildcard symbol? * is a specific routine in bash that returns the file and directory names of the current (or spcified) directory? Does * run different routines based on context- I'm still a bit confused what * actually means to bash. Feb 14, 2016 at 18:49
  • 1
    @user1028270 In Windows, the * is interpreted by the particular command (dir). Here, it is interpreted by the shell (bash), and not the command (ls). They mean the same thing, but are applied in different places during execution.
    – muru
    Feb 14, 2016 at 18:51
1

By default, if you invoke ls with a directory argument it displays the directory's contents. You can alter that behavior with the -d option. From man ls

-d, --directory
       list  directory entries instead of contents, and do not derefer‐
       ence symbolic links

You must log in to answer this question.

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