This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “Deleting and Renaming files: rm and mv Command”.
1. Which command is used for removing/deleting files in UNIX?
Explanation: rm command is used in UNIX to remove one or more files. It operates silently and should be used with caution. The filename of the file to be deleted is provided as an argument to rm command. For example, following command deletes abd.txt file.
$ rm abd.txt
2. We can delete multiple files using a single rm command.
Explanation: Multiple files can be deleted in a single go using rm command. All we need to do is, provides multiple filenames as arguments to the rm command.
$ rm file1 file2 file3
3. To delete all files in a directory we use ______________
a) rmdir *
b) mv *
c) rm *
d) del *
Explanation: To delete all files in a directory, use (*) with rm command. It acts as a meta-character and delete all files without displaying any message on the terminal. So use this command cautiously.
4. Some files cannot be deleted using rm command because of the permissions associated with it.
Explanation: Whether we are able to remove a file depends on the permissions associated with the file/directory. So sometime it may happen that permissions of a file won’t allow us to delete it.
5. Which option is used with rm command for interactive deletion?
Explanation: Like in cp command, -i option is also used with rm command for interactive deletion. The prompts asks the user for confirmation before deleting the files.
$ rm file1 file2 file3 rm: remove file1 (yes/no)? ?y rm: remove file1 (yes/no)? ?n rm: remove file1 (yes/no)? [Enter] // any other response other than y/n leaves the file undeleted
6. Which option performs recursive deletion?
d) -r and -R
Explanation: With -r or -R option, rm performs a recursive walk in the file hierarchy and searches for every subdirectories and file within this directory, At each stage, it keeps on deleting everything it finds.
7. Which one of the following command can delete a directory which is not empty?
a) rm -r
c) rm *
d) del *
Explanation: rm command normally does not remove directories but when it is used with -r option it does. So if we invoke a command like,
$ rm -r * // deletes all files in the current directory and all its subdirectories
8. If we wish to delete a remove a file forcefully, we can use ____ option with rm command.
Explanation: If a file is write-protected, we can remove it forcefully using -f option of rm command. For example,
$ rm -f file.txt // removes file.txt forcefully
9. Which command is used for removing file named -file.txt?
a) rm -file.txt
b) rm file.txtrm
c) rm — -file.txt
d) rm -f file.txt
Explanation: To delete a file with filename beginning with a ” – “, we have to use ( — ) i.e. double-dash. ” — ” is used so that rm command does not misinterpret the filename as an option.
10. Which command is used for renaming files?
Explanation: mv command is used for renaming files. This command does not create a copy of the file, it simply renames it. To rename file1 to file2 simply type,
$ mv file1 file2
11. If the destination file does not exists, then mv command creates it.
Explanation: If the destination file specified in the mv command does not exists it will be simply created. For example, if in the command mv file1 file2, if file2 does not exists then it will be created. Now file1 will be known as filename ‘file2’.
12. What if the destination file specified in mv command already exists?
a) it will be deleted
b) it will not be affected
c) it will be overwritten
d) an error will be produced
Explanation: If the destination file already exists, then the contents of this file will be overwritten with the contents of a source file. For example, mv file01 dir01. If there already exists a file named dir01, then the contents of dir01 will be overwritten with the contents of file01.
13. Apart from renaming files, mv command can also
a) move a file
b) move a group of files
c) rename a directory
d) move a group of files and rename a directory
Explanation: Apart from renaming files, mv command can also move a group of files to a different directory. For example,
$ mv abd.txt mash.txt ryan.txt main_dir
Above command moves three files to main_dir directory.
14. The inode number associated with a file is changed after renaming it.
Explanation: Every file has an inode number associated with it which is maintained by the kernel. This number is not disturbed even if we rename the file. It remains the same no matter how many times we rename the file.
15. Which option is used for backing up destination file in mv command?
Explanation: mv command renames the destination file if it already exists but when -b is used, it will add a suffix to the filename. This will save a copy of the original file instead of overwriting it. The default suffix is ( ~ ).
16. Which option is used with mv command so that the destination file does not get overwritten?
Explanation: If the destination file already exists in mv command then it will be overwritten which can be harmful. To avoid this we use -n option with mv so that the destination file does not get overwritten. -i option displays interactive messages and asks the user before overwriting the destination file.
17. -f option when used with mv command overwrites existing files without prompting any messages.
Explanation: When -f option is used with mv command then, the system will not display any diagnostic messages on to the terminal. It will simply replace the existing file without any warning.
Sanfoundry Global Education & Learning Series – Unix.
To practice all areas of Unix, here is complete set of 1000+ Multiple Choice Questions and Answers.