Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use grep in the terminal to print a list of files matching a specific grep pattern?

For a school project, I have to SSH into a folder on the school server, the usr/bin folder which has a list of files, then print a list of files that start with "file". I know Regex half-decently, at least conceptually, but I'm not sure of the UNIX command to do this.

I tried grep '^[file][a-zA-Z0-9]*' (start of a line, letters f-i-l-e, then 0 or more occurrences of any other number or digit) but that doesn't seem to work.

Help?

like image 362
Doug Smith Avatar asked Dec 21 '22 03:12

Doug Smith


2 Answers

You can use find command for this once you are connected to your school server.

find /usr/bin -type f -name "file*"

How would I do it if I wanted all files that started with a OR b, and ended with a OR b

Using find:

find /usr/bin -type f -regex "^[ab].*[ab]$" 

Using ls and grep:

ls -1 /usr/bin | grep "^[ab].*[ab]$"
like image 146
jaypal singh Avatar answered Dec 24 '22 01:12

jaypal singh


You should be able to use a simple ls command to get this information.

cd /usr/bin
ls -1 file*
like image 25
Wesley Womack Avatar answered Dec 24 '22 01:12

Wesley Womack