Unix Questions and Answers – File Handling Commands: pwd, cd

This set of Unix Multiple Choice Questions & Answers (MCQs) focuses on “File Handling Commands: pwd, cd”.

1. Which command is used for printing the current working directory?
a) HOME
b) cd
c) pwd
d) dir
View Answer

Answer: c
Explanation: pwd command is used for checking our current directory. Current directory is the directory in which we are currently working. pwd displays the absolute pathname i.e. with respect to the root directory.

$ pwd
/home/user06/Abdullah

advertisement
advertisement

2. Which command is used for changing the current directory?
a) cd
b) cp
c) pwd
d) rm
View Answer

Answer: a
Explanation: cd (change directory) command is used for moving around the file system. cd command is usually invoked with a argument. After invocation, it changes the current directory to the directory specified as argument. Cp command is used for copying files while rm command is used for deleting files.
For example: our current directory is /bin/user06 and we want to change our directory to a directory named dir_one which is inside the user06 directory. To do so, type the following:

$ cd dir_one
$pwd
/bin/user06/dir_one

3. cd command cannot be used without any argument.
a) True
b) False
View Answer

Answer: b
Explanation: cd command can be used without any arguments. When it used in such a way, then it changes our current directory to home directory.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

4. Which command is used for creating directories?
a) rmdir
b) mkdir
c) cd
d) cp
View Answer

Answer: b
Explanation: Directories in UNIX are created using mkdir command. The name of the directory to be created is specified as an argument to the mkdir command. For example, to create a dir named dir_01 in the current directory we can use the following command,

advertisement
$ mkdir dir_01

5. We can create multiple directories by single invocation of mkdir command.
a) True
b) False
View Answer

Answer: a
Explanation: Multiple directories can be created by one mkdir command.

advertisement
$ mkdir dir_01  dir_02  dir_03

6. What does the following command do?

    $ mkdir dir   dir/dir_01/dir_02

a) create dir, dir_01 and dir_02
b) creates dir_02
c) creates dir only
d) throws an error
View Answer

Answer: a
Explanation: The above command first creates a directory named dir and after that it creates a subdirectory dir_01 under dir. At last, it creates another subdirectory dir_02 under dir_01. Thus a directory tree is formed in which directory dir is the parent directory and dir_01, dir_02 are subdirectories.

7. Sometimes we are unable to create a directory because ______________
a) the directory may already exist in the current directory
b) there may be an ordinary file by the same name in the current directory
c) the permissions set for the current directory does not allow the creation
d) the directory may exist, there may be an ordinary file, the permissions set for the current directory does not allow the creation
View Answer

Answer: d
Explanation: Sometimes we are not able to create directory because of multiple reasons as stated above.

8. Which command is used for removing an empty directory?
a) mkdir
b) rmdir
c) del
d) remove
View Answer

Answer: b
Explanation: rmdir command is used for removing directories provided the directory should be empty. For example, to remove a directory named dir_001 in the current directory type the following command on the terminal.

$ rmdir  dir_001

9. Multiple directories can be removed using single rmdir command.
a) True
b) False
View Answer

Answer: a
Explanation: Like mkdir command, we can delete multiple directories using one shot of rmdir command. While deleting directories and subdirectories, a reverse logic is applied i.e. first the subdirectories or the child directories are removed and then their parent directories.

$  rmdir  dir   dir/dir_01/dir_02

10. For creating or removing directories, the user must be positioned above the directory or in the parent directory of the directory, on which the operation is to perform.
a) True
b) False
View Answer

Answer: a
Explanation: For performing any operation on the file system, the user must be hierarchically above the directory or should be in parent directory of the directory on which the operation is to be performed. Without following this rule, the user will not be able to perform any operation on the file system.

11. If rmdir dir001 fails, what could be the reason(s)?
a) dir001 doesn’t exist
b) dir001 is not empty
c) permissions of dir001 doesn’t allow to remove it
d) dir001 doesn’t exist, dir001 is not empty and permissions of dir001 doesn’t allow to remove it
View Answer

Answer: d
Explanation: There could be multiple reasons which could lead to failure while removing a directory. Major reasons could be:
-> directory doesn’t exists.
-> directory is not empty.
-> permissions of directory doesn’t allow to remove it.

12. pwd and echo $HOME will display the same output.
a) True
b) False
View Answer

Answer: b
Explanation: pwd is used for displaying the absolute pathname of our current working dircectory while $HOME displays the absolute pathname of our home directory.

$ echo  $HOME
/home/user08			    // displays home directory
$ pwd
/home/user08/documents/template	   // displays current directory

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.