List only specific files in the directory corresponding to the word BOZO and ending with "123" or "456",

I am trying to figure out how to get a list of file names for a file named BOZO, but ending with ONLY 123 OR 456.

Files:

BOZO12389,
BOZOand3
BOZOand456
BOZOand5
BOZOhello123

Thus, the command should only display "BOZOhello123" and "BOZOand456"

I can not understand. I have tried all forms of LS and GREP that I can think of. The funny thing is: we tried to do this in the class in about 10 minutes, and no one was able to get it (including the instructor).

+7
source share
5 answers

I did the following and it worked

ls BOZO*456 BOZO*123 
+10
source

Using shell globs:

 ls BOZO*{123,456} 
+8
source

Use regular expressions to help you. The egrep command should help as it allows you to use regular expressions.

You are looking for files like BOZO456 and BOZO123

Period . is a wild card that allows you to replace <anything> . * Allows you to repeat it 0 or more times. By placing around 123 and 456 parentheses, you will simulate OR.

Thus, you want any character to be repeated 0 or more times, then 123 or 456.

Example:

egrep "BOZO.*(456|123)" data

Thanks to Nathan Fellman for help and corrections.

+2
source

You can also use the find command:

  find . \( -name "BOZO*123" -o -name "BOZO*456" \) 
+1
source

It shouldn't be that hard. The most naive way is the ls directory and then grep just for what you want:

 $ ls *BOZO* | grep -e '123$' -e '456$' 
0
source

All Articles