250+ TOP MCQs on Copying a File: cp Command and Answers

Unix Multiple Choice Questions on “Copying a File: cp Command”.

1. What is the function of cp command in UNIX?
a) list all the available files in the current directory
b) delete a given file
c) cp is a command used for copying files and directories
d) change the directory

Answer: c
Clarification: cp command is basically used for creating a copy of source file or a group of files. The syntax of the command requires at least two filenames to be specified. If both the files specified are ordinary files, the first file will be copied to the second file. The syntax of cp command is cp source_filename destination_filename. The contents of the source file will be copied to the destination file in the same directory.

$ cp  file_01  file_02               // copies file_01 to file_02

2. What happens if the destination file specified in cp command does not exist?
a) file will not be copied
b) an error will be produced
c) destination file will be automatically created
d) none of the mentioned

Answer: c
Clarification: If the destination file does not exist, then cp command will automatically create a file with the same name and then it copies the contents of the source file to the file which is created. If the destination file already exists, then it will be overwritten with the contents of the source file.

3. Which of the following is not an option of cp command?
a) -z
b) -i
c) -R
d) -u

Answer: a
Clarification: cp command has many options available, each of which performs a unique task. (-i) is used for interactive copying (i.e.) it warns the user before overwriting the destination file. (-R) is used for copying the entire directory structure into another directory structure. (-u) copies only when the source file is newer than the destination file.

4. What is the correct syntax for copying multiple files with a filename starting as ‘file’ into another file named as ‘directory_one’?
a) cp -i file directory_one
b) cp -R file directory/directory_one
c) cp file* directory_one
d) none of the mentioned

Answer: c
Clarification: For copying multiple files with a common starting name such as (file, file001, file.txt, fileone.jpg, file-archive.zip) we use (“*”). An asterisk (“*”) is a wildcard – a special character which expands to match other characters. For example, cp file* directory_one will copy all the files whose name will be starting with ‘file’ into ‘directory_one’ file.

5. How can we copy an entire directory under another directory?
a) using -R option
b) using -a option
c) using -u option
d) none of the mentioned

Answer: a
Clarification: -R option is used to copy an entire directory structure into another one recursively. Here recursively means that the command can descend a directory and examine all the files in its subdirectories and then it will copy the entire structure. For example, cp -R prog_one new_prog. It will create a copy of contents of entire directory prog_one to new_prog if new_prog does not exist. Otherwise, if new_prog exists as a directory, then the whole structure of prog_one will be copied under new_prog as a subdirectory.

6. How can we copy a file into our current directory?
a) cp file1
b) cp file1
c) cp file*
d) none of the mentioned

Answer: b
Clarification: For creating a copy of a file into our current working directory we use the shorthand notation (.). For example, cp file1 . will create a copy of file1 into our current working directory with the same name.

7. What does the following command do?

a) copy all files to directory dir_file
b) update all files
c) delete all files
d) update all files in the current working directory and copy newer ones to directory dir_file

Answer: d
Clarification: (-u) option is used with cp command to update files and copy only when the source file is newer than destination file. So the above command will update all files in the current working directory and copy newer ones to directory dir_file.

8. Sometimes it is not possible to copy a file.
a) True
b) False

Answer: a
Clarification: Sometimes it may happen that we cannot copy a file because of the permissions associated with it. For example, If the permissions associated with a file are read-protected then we cannot copy the file.

9. What does -i option do?
a) interactive copying
b) recursively copying
c) updating
d) none of the mentioned

Answer: a
Clarification: (-i) option is used for interactive copying. It means that suppose if we invoke a command like cp file1 file2 and if file2 already exists then it will be overwritten which can be a nightmare for many users. To avoid this, we use -i option. It provides us with a warning before overwriting a file. It asks the user whether to overwrite the file or not. The syntax is cp -i source_file dest_file.

$ cp  -i  file_01  fo_02
cp: overwrites fo_02 (yes/no)?             // asks before overwriting

10. To copy multiple files, the last destination file should be a directory.
a) False
b) True

Answer: b
Clarification: We can copy multiple files using a single invocation of cp command. To do so, the last name specified should be a directory. For example, cp file1 file2 file3 dir_one. It will copy all the three files to the directory named dir_one.

11. -n option is used with cp command for what purpose?
a) existing file should not be overwritten
b) to update file
c) interactive copying
d) recursive copying

Answer: a
Clarification: -n option is used with cp command when we do not want to want to overwrite the existing file. It means that if the destination file already exists, then it should not be overwritten.

12. Which option is used with cp command for linking files instead of copying?
a) -v
b) -l
c) -f
d) -x

Answer: b
Clarification: -l option is used with cp command for linking files instead of copying. When one file has more than one filenames, we say that the file has more than one link. This file can be accessed by more than one filename.

13. $ cp -f copies forcefully.
a) True
b) False

Answer: a
Clarification: -f option copies forcefully when the permissions associated with a file does not allow performing the operation.

14. -v option is used with cp command for displaying _____
a) errors
b) informative messages
c) diagnostic messages
d) file contents

Answer: b
Clarification: cp -v displays the informative messages while copying files about what is happening. For example,

Leave a Reply

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