0

I tried ls ?[0-9]* , this matches all files in current directory, but also expands any directories in my current directory as and matches the files in any sub directory. I want * to match only to current directory.

---- Program output --------------------------------
f1
f2
g2t
g3t

d2:

d4:

--- Expected output (text)---
d2
d4
f1
f2
g2t
g3t

Here d2 and d4 are directories, while others are files. I want them to be listed as d2 and d4 and not as d2: and d4:

kaliko
  • 583
asdf
  • 103

2 Answers2

1

?[0-9]* only expands to names in the current directory that has a digit as their second character. ls, when getting a directory name, will obviously show the contents of that directory, which means that you will have to tell ls to not not do that.

You do this with ls -d:

ls -d -- ?[0-9]*

or

ls -d ./?[0-9]*

The -- and ./ will stop ls from interpreting the first character of the filename as an option if it starts with a dash.

If you are only interested in non-directories (and want to also weed out symbolic links to directories), then there are two immediately obvious ways of doing this.

  1. Use a shell loop, testing each name to see whether it's a directory or not and display the name if it isn't:

    for name in ?[0-9]*; do
        [ ! -d "$name" ] && printf '%s\n' "$name"
    done
    

    To include files that start with a dot (hidden files), a shell may have a dotglob shell option that you may set before the loop (shopt -s dotglob in bash).

  2. Use find to look for non-directories (and weed out symbolic links to directories) in the current directory:

    find -L . -maxdepth 1 ! -type d -name '?[0-9]*'
    

    The -L option to find makes -type d refer to the target of any symbolic link (if the current pathname is a symbolic link).

Kusalananda
  • 333,661
  • Beware that on many systems and in some locales, [0-9] matches on much more than 0123456789. [[:digit:]] should be OK, use [0123456789] to be sure. – Stéphane Chazelas Jun 18 '18 at 05:49
  • Beware that in most Bourne-like shells, if there's no matching file, you may end up listing the ?[0-9]* file whose second character is not a digit. That's a misfeature introduced by the Bourne shell, fixed again in zsh, or in bash with the failglob option. – Stéphane Chazelas Jun 18 '18 at 05:51
-1

You could pipe ls to egrep using a regex like so:

ls | egrep '.\d.*'
Joe M
  • 876