MCQs on Working with Files and Directories | Shell Scripting

In shell scripting, working with files and directories is crucial for automating tasks. This chapter explores file creation, viewing, deletion, directory management, permissions, ownership, wildcards, and file globbing techniques.


Chapter: Working with Files and Directories in Shell Scripting – MCQs

1. File Creation, Viewing, and Deletion

  1. Which command is used to create an empty file in Linux?
    • a) touch filename
    • b) create filename
    • c) make filename
    • d) new filename
  2. What command is used to view the contents of a file in Linux?
    • a) view filename
    • b) open filename
    • c) cat filename
    • d) read filename
  3. Which command is used to delete a file in Linux?
    • a) delete filename
    • b) remove filename
    • c) rm filename
    • d) erase filename
  4. Which of the following commands will display the contents of a file page by page?
    • a) more filename
    • b) cat filename
    • c) open filename
    • d) view filename
  5. What happens when you use the rm command without the -f option?
    • a) It deletes the file without any confirmation
    • b) It asks for confirmation before deleting the file
    • c) It does not delete the file
    • d) It renames the file instead of deleting
  6. Which command would you use to create a file named example.txt and add the string “Hello World” into it?
    • a) touch example.txt
    • b) echo "Hello World" > example.txt
    • c) create example.txt "Hello World"
    • d) echo > example.txt "Hello World"
  7. Which command is used to remove a directory in Linux?
    • a) rmdir
    • b) rm -r
    • c) del
    • d) remove
  8. If you want to remove a file forcefully without prompting for confirmation, which command would you use?
    • a) rm -i filename
    • b) rm -f filename
    • c) rm filename -force
    • d) remove -f filename
  9. What does the command cat do?
    • a) Copies a file to another location
    • b) Displays the contents of a file
    • c) Creates a file
    • d) Deletes a file
  10. What will the command touch myfile do?
    • a) Create an empty file named myfile
    • b) Display the contents of myfile
    • c) Delete the file myfile
    • d) Edit the file myfile

2. Directory Management

  1. How do you create a new directory in Linux?
    • a) mkdir directoryname
    • b) create directoryname
    • c) dir directoryname
    • d) make directoryname
  2. Which command is used to list the contents of a directory?
    • a) dir
    • b) ls
    • c) list
    • d) cat
  3. Which option with the ls command will show hidden files?
    • a) -l
    • b) -a
    • c) -h
    • d) -v
  4. How do you navigate into a directory named Documents in Linux?
    • a) go Documents
    • b) move Documents
    • c) cd Documents
    • d) cd /Documents
  5. What does the pwd command do?
    • a) Prints the working directory
    • b) Changes the working directory
    • c) Prints the current file
    • d) Lists the contents of the working directory
  6. Which command is used to remove an empty directory?
    • a) rmdir directoryname
    • b) rm directoryname
    • c) delete directoryname
    • d) remove directoryname
  7. Which command would you use to change to the parent directory in Linux?
    • a) cd ..
    • b) cd /
    • c) cd ~
    • d) cd ../
  8. How can you list the contents of a directory in a detailed format in Linux?
    • a) ls -l
    • b) dir -l
    • c) ls -d
    • d) dir -a
  9. Which command is used to rename a directory in Linux?
    • a) mv oldname newname
    • b) rename oldname newname
    • c) rename directory oldname newname
    • d) move oldname newname
  10. What is the purpose of the cd command?
    • a) Change the directory
    • b) Create a new directory
    • c) List the current directory
    • d) Delete the current directory

3. Permissions and Ownership

  1. Which command is used to change file permissions in Linux?
    • a) chmod
    • b) chown
    • c) chperm
    • d) setperm
  2. How can you change the owner of a file in Linux?
    • a) chown
    • b) chmod
    • c) chgrp
    • d) chfile
  3. Which option with the chmod command is used to give the owner execute permission?
    • a) +x
    • b) -x
    • c) +r
    • d) +w
  4. What command would you use to change the group ownership of a file?
    • a) chown user:group filename
    • b) chmod user:group filename
    • c) chgrp group filename
    • d) chown group filename
  5. What does the command chmod 777 filename do?
    • a) Gives read, write, and execute permissions to everyone
    • b) Removes all permissions from the file
    • c) Gives only read permission to the owner
    • d) Gives only write permission to the group
  6. Which of the following represents the permission setting for a file that allows read, write, and execute permissions for the owner, and read permissions for the group and others?
    • a) 755
    • b) 777
    • c) 644
    • d) 555
  7. What command shows the permissions of a file in Linux?
    • a) ls -l
    • b) permissions filename
    • c) file permissions filename
    • d) ls -a
  8. Which of the following allows you to recursively change the permissions of all files in a directory?
    • a) chmod -R
    • b) chmod -r
    • c) chmod -a
    • d) chmod -all
  9. What does the umask command do?
    • a) Sets default file permissions
    • b) Changes the owner of a file
    • c) Sets group permissions
    • d) Displays the current permissions of a file
  10. Which command displays the ownership of a file?
    • a) ls -l
    • b) stat filename
    • c) ls -o filename
    • d) chmod -l filename

4. Wildcards and File Globbing

  1. What does the * wildcard do in Linux?
    • a) Matches zero or more characters in a filename
    • b) Matches a single character in a filename
    • c) Matches all files in the current directory
    • d) Matches hidden files
  2. Which wildcard is used to match a single character in a filename?
    • a) *
    • b) ?
    • c) []
    • d) {}
  3. What does the [] wildcard do in Linux?
    • a) Matches a single character from a set of characters
    • b) Matches zero or more characters
    • c) Matches a single directory
    • d) Matches all files with a specific extension
  4. Which command would list all .txt files in a directory?
    • a) ls *.txt
    • b) ls txt*
    • c) ls *.text
    • d) ls .txt
  5. What will ls file?.txt match?
    • a) Files like file1.txt, fileA.txt
    • b) Files like file.txt, file1.txt
    • c) Files like file11.txt
    • d) All files starting with file
  6. What does the wildcard ?.txt match in Linux?
    • a) Files that start with one character followed by .txt
    • b) Files that contain a question mark in the name
    • c) Files that are 3 characters long
    • d) Files with no extension
  7. What does the {a,b,c} wildcard match in a filename?
    • a) Files that match either a, b, or c
    • b) Files that start with a and end with b
    • c) Files that contain a, b, or c anywhere in the name
    • d) Files with an exact match of a, b, or c
  8. Which of the following will match files like file1.txt and file2.txt?
    • a) file?.txt
    • b) file*
    • c) file*.txt
    • d) file[1-2].txt
  9. What does ls *[a-z].txt do?
    • a) Lists all .txt files with a lowercase letter at the end
    • b) Lists all .txt files starting with a lowercase letter
    • c) Lists all files containing .txt
    • d) Lists all files
  10. Which command is used to check if a pattern matches multiple filenames?
    • a) ls
    • b) echo
    • c) find
    • d) test

Answers

QnoAnswer
1a) touch filename
2c) cat filename
3c) rm filename
4a) more filename
5b) It asks for confirmation before deleting the file
6b) echo "Hello World" > example.txt
7b) rm -r
8b) rm -f filename
9b) Displays the contents of a file
10a) Create an empty file named myfile
11a) mkdir directoryname
12b) ls
13b) -a
14c) cd Documents
15a) Prints the working directory
16a) rmdir directoryname
17a) cd ..
18a) ls -l
19a) mv oldname newname
20a) Change the directory
21a) chmod
22a) chown
23a) +x
24a) chown user:group filename
25a) Gives read, write, and execute permissions to everyone
26a) 755
27a) ls -l
28a) chmod -R
29a) Sets default file permissions
30a) ls -l
31a) Matches zero or more characters in a filename
32b) ?
33a) Matches a single character from a set of characters
34a) ls *.txt
35a) Files like file1.txt, fileA.txt
36a) Files that start with one character followed by .txt
37a) Files that match either a, b, or c
38a) file?.txt
39a) Lists all .txt files with a lowercase letter at the end
40b) echo

Use a Blank Sheet, Note your Answers and Finally tally with our answer at last. Give Yourself Score.

X
error: Content is protected !!
Scroll to Top