In one of my lab questions I was asked to list all entries(both files and folders) in the current directory whose names contain only uppercase letters using piping and grep command. Folders must have a forward slash displayed. Initially, my solution looked like this:
ls -p | grep -e "^[[:upper:]]*\/?$"
I thought that I can protect the '/' sign with the backward slash(as in echo \*
vs echo *
). However, it doesn't work as intended.
One of my friends suggested the following solution:
ls -p | grep -e '^[[:upper:]]*/\?$'
I have no idea why the backward slash is used like this. What is even more weird,for me, that it actually produces the right result. I was wondering if someone could explain where the mistake in my solution is and why the second version actually works.
ls
?grep
for that task , so that's what I went for. However, I would be grateful to see another way to solve it