0

How do you list only files that have two numbers in the name?

I did ls *[0-9]*[0-9]* but it appears that this is not the right one as it shows other files that have 4 digits in their name.

How do you list only files are two-letter-named txt files?

for this I did ls ??.txt but I do not know how to list only letter named files.

I am using bash.

0
2

In *[0-9]*[0-9]*, the asterisks can match anything, so even more letters. Something like 1234.txt does have two digits in it, even if that's not how it would be usually phrased in English.

You'd need something that matches "any number of characters that are not digits" to replace the asterisks, and that's harder to do with regular globs. You could do it with ksh-style extended globs, though. In e.g. Bash:

#!/bin/bash
shopt -s extglob
ls *([^0-9])[0-9]*([^0-9])[0-9]*([^0-9])

(An alternative would be to loop over the list of filenames matching *[0-9]*[0-9]*, and filter out the ones that also match *[0-9]*[0-9]*[0-9]*. Either way, it's not very pretty.)

Any two letters (only), followed by .txt would be [[:alpha:]][[:alpha:]].txt.

1
  • thanks, but I'm not sure about the second question because it doesn't show me any results
    – user462170
    Mar 23 '21 at 18:09

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.