4
 ls "*"

This shows nothing in my directory. Why?

Isn't * a wildcard that will show everything?

  • 2
    Remember that it is the shell which expands *, not ls itself. When you put * in double-quotes, you ask the shell to pass it verbatim to ls. – isanae Jun 22 '17 at 23:37
19

When it's in double quotes the * doesn't get treated as a glob, and so doesn't get expanded. So you're asking ls to list a file named *, which probably doesn't exist.

To see all the files, you could run ls without any arguments as it's default behavior is to show you all the files in the current directory. If you wanted to pass all the files as arguments to ls for some reason, just remove the quotes so you run

ls *

but that's really similar to

ls

except that if you have a lot of files * might expand to pass too many arguments to ls, and also ls * will show the contents of directories while ls by itself will just show that the directories are in the current directory without descending into them.

  • 4
    A big difference between ls and ls * is when there are some directories along the files: The first will only print the directories' names, but the latter will also print their content. ls -d * is more similar to ls and may be more appropriate then. – mpy Jun 22 '17 at 22:36
  • 1
    @mpy indeed, perhaps waiting for the last sentence was burying that too far – Eric Renouf Jun 22 '17 at 22:37
  • Oh, sorry, my bad... +1 – mpy Jun 22 '17 at 22:38
  • Well, to be technically correct, neither ls or ls -d * will show all files. No dot-files or . and .. will be shown. The simplest option is ls -a, a shell equivalent implies changing dotglobdot or globignore or some other more complex items (not possible to cover in a comment, sorry). Maybe this will be enough for simple solutions: echo * .[!.]* ..?* (yes, not so simple, I know, but the others are even longer to explain). – Arrow Jun 23 '17 at 3:29
-7
ls *.* /Dir (dir is optional) 

lists every file(regardless of file type) in either the users current working directory (found by pwd) or in /Dir and

ls * /Dir (dir optional)

lists every file and directory one below in either the user's current working directory or in /Dir. However,

ls "*" & ls *.* 

will not list recursively, for that you need

ls -R *.* /Dir

or

ls -R "*" /Dir

As for why

ls "*"

is not working correctly, you are correct: * is a wildcard, and when you call ls as so:

ls "*"

you are asking bash to find a file and/or directory that has an asterisk in its name. I had installed an IDE and could not find the configuration and main files for it, so I used these commands to find the files:

(found files matching the pattern and redirected them to a specific file)

locate *eclipse* >locate_eclipse.txt
locate *tar* > locate_tar.txt
locate *.tar* > locate_".tar".txt
locate *compiler* > locate_compiler.txt
locate *JRE* > locate_JRE.txt
locate *jre* > locate_jre.txt

(Searched the whole filesystem and only prints the ones that match the pattern)

ls -R / | grep "eclipse"
ls -R / | grep "tar"
ls -R / | grep ".tar"
ls -R / | grep "compiler"
ls -R / | grep "JRE"

Sorry for the spiel, but I tried to cover most of the bases.

  • 10
    ls *.* does not list every file. It lists every file which has a period in its name. This pattern is not treated specially, as it is in DOS: In Bourne shells, it works the same as *a*. (In DOS, the . is sort of equivalent to an empty string.) – jpaugh Jun 23 '17 at 1:31
  • 7
    This answer is all sorts of wrong: *.* is wrong, ls -R "*" /Dir is wrong, "has an asterisk in its name" is wrong and parsing ls is wrong. – isanae Jun 23 '17 at 2:22
  • 4
    I wish I could downvote your answer multiple times, for (1) saying *.* in a Unix context (without explaining what it really means), (2) saying things like ls *.* /Dir (dir is optional), ls "*" & ls *.*, and ls -R "*" /Dir, which are, as far as I can tell, almost totally meaningless nonsense, (3) saying “when you call ls as ls "*", you are asking bash to find a file and/or directory that has an asterisk in its name.” (You are asking ls to find a file or directory that has an asterisk as its name.  To find a file that has an asterisk in its name, use *"*"*.) – G-Man Says 'Reinstate Monica' Jun 23 '17 at 2:22
  • 2
    (Cont’d) …  (4) showing locate commands with patterns with metacharacters (i.e., wildcards/globs) without quoting them, (4b) quoting the arguments to grep unnecessarily, (5) saying grep ".tar" without escaping the ., etc…. – G-Man Says 'Reinstate Monica' Jun 23 '17 at 2:22
  • 1
    @isanae: You ninja’d me! – G-Man Says 'Reinstate Monica' Jun 23 '17 at 2:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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