-2

I am in a folder where I do not have read permission in contents.

ls -l

List all files sucessfully.

I want to count files that have some attributes in their name with:

ls -l | grep file????.tar.gz | wc -l

The command above generates a lot of permission denied messages and finally I get no answer.

Why this happens and is there a workaround?

EDIT: Following Josip Rodin's Suggestion turns out I messed up using the wrong wildcard character. Executing:

ls -l | grep -e "file....\.tar.gz" | wc -l

worked like a charm

Mini Fridge
  • 149
  • 1
  • 5

1 Answers1

1

You're using the character ? in a sense that seems like you want it to mean a single-letter wildcard. However, that is the so-called "glob" pattern syntax, used/expanded by the shell executing those commands.

This probably results in the shell trying to look something up that it doesn't have permission to. (It would have been helpful if you included an example of such an error message.)

To actually do a single-letter wildcard match in grep, use the "regex" syntax supported by grep, where the single-letter wildcard character is the dot (.).

Alex
  • 3,129
  • 21
  • 28
Josip Rodin
  • 1,695
  • 13
  • 18
  • TBH, OP don't need grep at all. `ls file????.tar.gz | wc -l` should be enough – Alexey Ten Dec 02 '15 at 08:24
  • There's a myriad of more efficient ways, `find . -maxdepth 1 -name 'file????.tar.gz' | wc -l` or `ls -1 | grep -c 'file....\.tar\.gz'` come to mind. – Josip Rodin Dec 02 '15 at 15:13