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.
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?
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?
$ echo *
b) undefined behavior
c) displays “*”
d) lists all filenames in the current directory
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.
4. Which command would be most suitable to remove the following files?
dirx diry dirz dirzw
a) rm dir?
b) rm dirx diry dirz dirzw
c) rm *
d) rm dir*
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.
5. Which of the following files will not be deleted using “rm chap??” ?
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 (.)?
b) ls -a
c) ls .???*
d) ls *
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 ____
d) / and .
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 will delete all of the following files.
chap01 chap02 chap03 chap04
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 will delete chap01 chap02 chap03 chap04.
9. Which of the following files will not be listed using the following command?
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?
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?
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.
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.