This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “Displaying and Creating Files: cat Command”.
1. Which command is used for displaying contents of a file?
Explanation: cat command is used to display the contents of a file. For displaying the contents, simply invoke the cat command with the filename (whose contents are to be displayed) as an argument. For example, if abd.txt file contains “HELLO WORLD” then,
$ cat abd.txt HELLO WORLD
2. Apart from displaying file contents, cat command is also used for _____ files.
Explanation: cat command is one of the most well-known commands of the UNIX system. It is generally used to display the contents of a file on the terminal but can also be used for creating files. Suppose to create a file abd.txt, type the following command
$ cat > abd.txt
Now the prompt vanishes. cat command now waits for the input to be entered by the user. After the input is entered, the user hits the ctrl-D button which marks the end of the file and thus the file and the contents entered by the user are saved.
3. Multiple arguments as filenames can be specified in cat command.
Explanation: cat, like several other UNIX commands can accept more than one filename as arguments. For example,
$ cat file1 file2
Above command shows the contents of the second file immediately after the contents of the first file without any header information i.e. it concatenates the contents of two files on the terminal.
4. Which symbol is used with cat command for creating files?
Explanation: > symbol acts as a redirection symbol and records the input entered by the user in the specified file. For example,
$ cat > file_01 UNIX is a powerful operating system // input entered by the user [CTRL-D]
5. If we create a file using cat command with the same filename which already exists in the current directory then,
a) existing file is deleted
b) new file will be created separately
c) existing file will be overwritten
d) an error will be produced
Explanation: As cat command is also used to create files, so if we want to create a file with the same filename which already exists in the directory then the existing file will be overwritten.
6. Which symbol is used to append an existing file?
Explanation: cat command overwrites the existing file if we try to create another file with a similar filename as an existing file. To avoid this we can use the >> operator. For example, if file01 already exists and we want to append it, then use the following command:
$ cat >> file01
7. Which option is used with cat command for displaying non-printable characters?
Explanation: If the file that we want to display contains nonprintable ASCII characters, then to display those characters we use -v option with the cat command. -n option is used for displaying the contents of the file along with line numbers.
8. Which option is used with the cat command for displaying file with line numbers?
Explanation: cat command supports -n option which is used for displaying file contents along with line number while -v is used for displaying nonprintable ASCII characters in the file.
9. Which of the following cannot be performed by cat command?
a) displaying files
b) creating files
c) appending files
d) deleting files
Explanation: cat command cannot delete files. It can only be used for viewing file contents, creating a file or appending to an existing file.
10. What does cat file01 file01 file01 display?
b) blank terminal
c) contents of file01 three times successively
d) contents of file01 single time
Explanation: When multiple filenames are specified as arguments with cat command, it concatenates the contents of all the files specified as arguments and displays the output on the terminal.
11. Which files will be displayed by the following command:
a) all files in the directory
b) all files with filename containing ‘file’
c) no files will be displayed
d) a single file
Explanation: Since * is a meta-character, here it is being used for pattern matching. So above command will search all the files in the directory having ‘file’ as a part of their filename. After that it will concatenate the output of all the searched files and display them on the terminal.
12. Which command is used to create empty files?
Explanation: Touch command changes timestamps. It is also an easy way to create empty files.
The only argument required to be specified with touch command is the filename of the file to be created.
$ touch file01
13. Which option is used with touch command which forces the command not to create file, if it does not exists.
Explanation: In case if there is a strict requirement that touch command should not create a new file, we can use the -c option with it. Else if the file already exists then it will do nothing.
$ touch -c newfile001
14. Which one of the following commands is incorrect?
a) cat file01
b) cat > file01
c) cat >> file1
d) cat -a file01
Explanation: There is no such option as -a available with cat command.
cat file01 // display contents of file01 cat > file01 // create a new file named 'file01' cat >> file1 // append to the file named 'file01' cat -a file01 // invalid command
Sanfoundry Global Education & Learning Series – Unix.
To practice all areas of Unix, here is complete set of 1000+ Multiple Choice Questions and Answers.