0

How can I display all files in current directory where the first character is numeric and last character is non alphanumeric. I already try with :

ls grep -E '^{[-0-9] 

but this command line did not give me results as expected.

2
  • The line looks like it has errors; there is no pipe between the ls and grep, no close "'" bracket.. etc so I'd suggest ensuring you're asking us exactly what you tried, as it doesn't look like it'll work to me either.
    – guiverc
    Dec 3, 2019 at 9:45
  • 1
    Can you provide some filename example? It would be easier to understand which results you're expecting
    – damadam
    Dec 3, 2019 at 9:49

3 Answers 3

2

You don't need grep at all - you can us the bash shell's built-in pattern matching (globbing) feature. For example (assuming "numeric" in this context means a decimal digit):

ls [0-9]*[^0-9A-Za-z]

or

ls [[:digit:]]*[^[:alnum:]]

See for example

Note that if you want to view the files' contents as per your title, you will need something like cat (or perhaps head) in place of ls.

2
  • +1 I didn't know that we could use regex directly with ls command, and you can simplify A-Za-z with a-Z based on this answer
    – damadam
    Dec 3, 2019 at 11:17
  • 1
    @damadam it's actually not regex (note the * wildcard where regex would have .* for example). I'm cautious about simplifying because the expansions are locale-dependent, as explained in the manual that I linked. Dec 3, 2019 at 11:24
0

Try this:

mtodorov@efk:~$ ls | grep -E '^[0-9].*[^0-9A-Za-z]$'
0abc%
1abc%
mtodorov@efk:~$

Explanation: ^ marks the beginning and $ marks the end of a regular expression, .* is a wildcard like * in shell, ^ within brackets negates the selection and the single quotes must be closed.

4
  • based on the regex of this answer, you can simplify A-Za-z with a-Z, so you would have 0-9a-Z
    – damadam
    Dec 3, 2019 at 10:46
  • 1
    If you looked at the ASCII table, there are characters between the capital Z and small a, so you cannot simplify A-Za-z with a-Z. Plus a-Z selects nothing, you would use A-z.
    – gmt42
    Dec 3, 2019 at 11:02
  • a-Z is a condensed writing for a-zA-Z, where A-z would take character between Z and a (6 non alphabetical character); you must try this expression, you would be surprised
    – damadam
    Dec 3, 2019 at 11:14
  • 1
    It may work with GNU grep, but it is largely undocumented to work everywhere, and hereby not recommended for thenovice users. Here, for example, it isn't documented: rexegg.com/regex-quickstart.html#classes
    – gmt42
    Dec 3, 2019 at 11:32
0

Using regex matching with find:

$ find -maxdepth 1 -type f -regextype posix-extended \
   -regex '.*/[[:digit:]].*[^[:alnum:]]$' -printf %f\\n

find options:

  • -maxdepth n Descend at most n levels.
  • -type f Limit the type to regular files.
  • -regextype type RE syntax used by -regex.
  • -regex pattern RE pattern used to match the whole path.
  • -printf format will print formatted output.
  • .* Matches any character zero or more times.
  • [] Matches a single character contained within the brackets
  • [^] Matches a single character that is not contained within the brackets.
  • [:digit:] POSIX standard defined class for digits.
  • [:alnum:] POSIX standard defined class for alnum.
  • $ Matches end of line anchor.

You must log in to answer this question.

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