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?
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
2. Which command is used for changing the current directory?
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.
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.
4. Which command is used for creating directories?
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,
$ mkdir dir_01
5. We can create multiple directories by single invocation of mkdir command.
Explanation: Multiple directories can be created by one mkdir command.
$ 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
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
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?
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.
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.
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
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.
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.