so one of the questions i have been working on is to display the contents of a directory files by using its wildcards do display specific contents, in this case aim using ls and the portion that I got lost on is I need to display filenames that stat is a but exclude everything that has aa. it can however be ab.

any hints, ill keep looking and post and answer if i find anything, and any help with usage of wildcards would be great. Thanks

  • Err, a "wildcard" glob will be expanded by the shell prior to ls even being run, and (unless zsh EXTENDED_GLOB or such) there's not much you can do with them. Another option would be to pipe the listing to grep, but that's not using wildcards. – thrig Mar 10 '16 at 23:17
  • typically i used ls ? * ! [] to figure what filenames to display – Phantom1421 Mar 10 '16 at 23:37

the answer i found is ls a[!a]*

This will allow me to find a word with a as first character but nothing with aa

  • Your glob still won't match the one-letter filename a. I don't know if that's important to you. – Wildcard Mar 11 '16 at 1:45
  • It does exactly what I need it to display a file name that starts with 'a' but not 'aa' – Phantom1421 Mar 11 '16 at 1:47

Doing complicated file globs with multiple conditions in the shell is difficult. It's easier to use find for this:

find files -type f -maxdepth 1 -name "a*" \! -name "*aa*" -print

To get ls -l behaviour, change -print to -ls.

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.