250+ TOP MCQs on Pattern Matching, Escaping and Quoting and Answers

Unix Multiple Choice Questions & Answers (MCQs) on “Pattern Matching, Escaping and Quoting”.

1. Wildcards are special characters which are used to replace or represent one or more characters.
a) True
b) False

Answer: a
Clarification: A wildcard is a special character which can be used as a substitute for any of a class of characters, which increases the flexibility and efficiency of searching and replacing. For example, to remove all the files with a filename starting with ‘chap’ prefix, we can use the command rm chap*. Here * is used as a wildcard for matching all filenames starting with ‘chap’.

2. Which of the following is not a wild-card?
a) *
b) ?
c) $
d) %

Answer: c
Clarification: The ‘$’ sign represents the shell prompt while all the other characters belong to a category of shell wildcards.

‘*’     // matches any number of characters
‘?’    // matches a single character

3. What does the following command do?

a) error
b) undefined behavior
c) displays “*”
d) lists all filenames in the current directory

Answer: d
Clarification: When we use echo command with only * as the argument we simply see a list of file. All the filenames in the current directory are displayed on the terminal. Since we know that * is a wildcard that can match any number of character. Here it is used as solitary to match all filenames.

4. Which command would be most suitable to remove the following files?

a) rm dir?
b) rm dirx diry dirz dirzw
c) rm *
d) rm dir*

Answer: a
Clarification: Since we know that ? can be used to match a single character. In the above scenario, all the filenames are same except that the last character in all filenames is different. So we can use the ? meta-character.

5. Which of the following files will not be deleted using “rm chap??” ?
a) chap01
b) chap02
c) chaptd
d) chactd

Answer: d
Clarification: Since ? is used to match a single character, ?? can match two characters. So the above command will remove all files with a filename starting with a prefix ‘chap’, followed by any two characters.

6. Which of the following command will list all the hidden filenames in our directory having at least three characters after the dot (.)?
a) ls
b) ls -a
c) ls .???*
d) ls *

Answer: c
Clarification: The * doesn’t match all files beginning with a (.) dot. So if we need to lists all the hidden filenames in our directory having at least three characters after the dot (.) we can use the following command,

$ ls  .???*
.bash_pro    .chap01        .netspak    .profile        //list of hidden files

7. * and ? cannot match ____
a) /
b) $
c) .
d) / and .

Answer: d
Clarification: There are two things which * and ? cannot match. First is, they cannot match filenames starting with a dot (.). Second is, they cannot match / in the pathname. For example, we cannot use cd /usr?local to switch to /usr/local. It will generate an error.

8. rm chap0[1234] will delete all of the following files.

   chap01  chap02  chap03  chap04

a) True
b) False

Answer: a
Clarification: We can frame restrictive patterns with the character class. The character class comprises a set of characters enclosed by the rectangular brackets, [ and ], but it matches a single character in the class. For example, the pattern [abcd] is a character class that can match a single character – an a, b, c or d. Similarly, we can combine the character class with any string or any other wildcard expression. Hence, the command rm chap0[1234] will delete chap01 chap02 chap03 chap04.

9. Which of the following files will not be listed using the following command?

a) chap02
b) chap05
c) chap01
d) chap04

Answer: b
Clarification: Range specification is also possible inside the class using a hyphen (-). The two characters on either side of it form the range of characters to be matched. So the command ls chap0[1-4] can match chap01, chap02, chap03, chap04. But chap05 cannot be matched because the range inside the character class is from 1 to 4.

10. Which of the following symbol is used for negating the character class?
a) .
b) *
c) !
d) %

Answer: c
Clarification: We can use the ! as the first symbol for negating the character class. For example,

*.[!tx]        //matches all filenames with single character extension but not .t or .x 
[!a-zA-Z]*    // matches all filenames that don’t begin with a alphabetic character.

11. Which of the following shell doesn’t support ! symbol for negating the character class?
a) bash
b) bash
c) POSIX
d) C

Answer: d
Clarification: The ! symbol cannot be used to negate a character class in a C shell. In fact, C shell doesn’t provide any mechanism for doing so.

12. The command cd * will work.
a) True
b) False

Answer: a
Clarification: As we know that cd command is used for changing directories, the command cd * will work only when there is only one file in the current directory and that file should also be a directory. Otherwise, this command is invalid.

Leave a Reply

Your email address will not be published. Required fields are marked *