Bash – How to Grep by First Character

bashgrep

I'm trying to list all files and select only certain from them.
ls -1 return me the list

abc 
abcd
rm_0818-051752-753-06

after that I want to select only by this criteria to return the 3rd line so I do:

ls -1 | grep -E "^rm_.*"

but I get nothing.

I'm trying to recognize the first character of 3rd line in this way:

var=$(ls -1 | grep -E "rm_")
echo $var //returns rm_0818-051752-753-06
echo ${var:0:1} //returns some strange symbol 001B in square

Can you explain me this behavior and how could I grep by first character ^? Thanks

Best Answer

ls -1 | grep -E "^rm_.*" looks good and should work. Possible reason why it doesn't work is alias bound to ls command in your profile.
To ensure, try /bin/ls -1 | grep -E "^rm_.*"

Related Question