Unix Questions and Answers – Deleting and Renaming files: rm and mv Command

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?
a) rmdir
b) rm
c) del
d) mv
View Answer

Answer: b
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

advertisement
advertisement

2. We can delete multiple files using a single rm command.
a) True
b) False
View Answer

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

Note: Join free Sanfoundry classes at Telegram or Youtube

3. To delete all files in a directory we use ______________
a) rmdir *
b) mv *
c) rm *
d) del *
View Answer

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

4. Some files cannot be deleted using rm command because of the permissions associated with it.
a) True
b) False
View Answer

Answer: a
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?
a) -i
b) -f
c) -r
d) -R
View Answer

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

advertisement
$ 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?
a) -r
b) -R
c) *
d) -r and -R
View Answer

Answer: d
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
b) rmdir
c) rm *
d) del *
View Answer

Answer: a
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.
a) -i
b) -r
c) -R
d) -f
View Answer

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

Answer: c
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?
a) rename
b) mv
c) cp
d) move
View Answer

Answer: b
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.
a) True
b) False
View Answer

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

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

Answer: d
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.
a) True
b) False
View Answer

Answer: b
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?
a) -b
b) -f
c) -v
d) -i
View Answer

Answer: a
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?
a) -n
b) -f
c) -b
d) -i
View Answer

Answer: a
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.
a) True
b) False
View Answer

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

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.