Unix Questions and Answers – Pattern Matching, Escaping and Quoting – 1

This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “Pattern Matching, Escaping and Quoting – 1”.

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

Answer: a
Explanation: 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) %
View Answer

Answer: c
Explanation: 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?

advertisement
advertisement
$ echo *

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

Answer: d
Explanation: 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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

advertisement
dirx  diry  dirz  dirzw

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

Answer: a
Explanation: 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.
advertisement

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

Answer: d
Explanation: 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 *
View Answer

Answer: c
Explanation: 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 .
View Answer

Answer: d
Explanation: 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
View Answer

Answer: a
Explanation: 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?

ls chap0[1-4]

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

Answer: b
Explanation: 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) %
View Answer

Answer: c
Explanation: 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
View Answer

Answer: d
Explanation: 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
View Answer

Answer: a
Explanation: 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.

Sanfoundry Global Education & Learning Series – Unix.

To practice all areas of Unix, here is complete set of 1000+ Multiple Choice Questions and Answers.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.