Search code examples
bashscriptingwildcardls

Using ? wildcard with ls


I'm trying to use the ? wildcard to display only 1 character files, and ?.* to display 1 character files with extensions.

what works:

cd /mydir

ls ? ?.*

I'm trying to use this in a shell script so therefor i cant use "cd"

What i'm trying to get to work

ls ? ?.* /mydir

and it gives me the output:

ls: cannot access ?.*: No such file or directory

I've also tried:

ls /mydir ? ?.*

which gives me the exact same output as before.


Solution

  • When you write ls ? ?.* /mydir, you're trying to display the files matching three distincts patterns: ?, ?.*, and /mydir. You want to match only /mydir/? and /mydir/?.*, hence this command: ls /mydir/? /mydir/?.*.

    Edit: while this is a correct answer to the initial question (listing /mydir/? and /mydir/?.*), OP wanted to do this to parse the output and get the file count. See @gniourf_gniourf's answer, which is a much better way to do this.