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
Which command is used to create an empty file in Linux?
a) touch filename
b) create filename
c) make filename
d) new filename
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
Which command is used to delete a file in Linux?
a) delete filename
b) remove filename
c) rm filename
d) erase filename
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
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
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"
Which command is used to remove a directory in Linux?
a) rmdir
b) rm -r
c) del
d) remove
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
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
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
How do you create a new directory in Linux?
a) mkdir directoryname
b) create directoryname
c) dir directoryname
d) make directoryname
Which command is used to list the contents of a directory?
a) dir
b) ls
c) list
d) cat
Which option with the ls command will show hidden files?
a) -l
b) -a
c) -h
d) -v
How do you navigate into a directory named Documents in Linux?
a) go Documents
b) move Documents
c) cd Documents
d) cd /Documents
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
Which command is used to remove an empty directory?
a) rmdir directoryname
b) rm directoryname
c) delete directoryname
d) remove directoryname
Which command would you use to change to the parent directory in Linux?
a) cd ..
b) cd /
c) cd ~
d) cd ../
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
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
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
Which command is used to change file permissions in Linux?
a) chmod
b) chown
c) chperm
d) setperm
How can you change the owner of a file in Linux?
a) chown
b) chmod
c) chgrp
d) chfile
Which option with the chmod command is used to give the owner execute permission?
a) +x
b) -x
c) +r
d) +w
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
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
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
What command shows the permissions of a file in Linux?
a) ls -l
b) permissions filename
c) file permissions filename
d) ls -a
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
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
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
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
Which wildcard is used to match a single character in a filename?
a) *
b) ?
c) []
d) {}
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
Which command would list all .txt files in a directory?
a) ls *.txt
b) ls txt*
c) ls *.text
d) ls .txt
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
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
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
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
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
Which command is used to check if a pattern matches multiple filenames?
a) ls
b) echo
c) find
d) test
Answers
Qno
Answer
1
a) touch filename
2
c) cat filename
3
c) rm filename
4
a) more filename
5
b) It asks for confirmation before deleting the file
6
b) echo "Hello World" > example.txt
7
b) rm -r
8
b) rm -f filename
9
b) Displays the contents of a file
10
a) Create an empty file named myfile
11
a) mkdir directoryname
12
b) ls
13
b) -a
14
c) cd Documents
15
a) Prints the working directory
16
a) rmdir directoryname
17
a) cd ..
18
a) ls -l
19
a) mv oldname newname
20
a) Change the directory
21
a) chmod
22
a) chown
23
a) +x
24
a) chown user:group filename
25
a) Gives read, write, and execute permissions to everyone
26
a) 755
27
a) ls -l
28
a) chmod -R
29
a) Sets default file permissions
30
a) ls -l
31
a) Matches zero or more characters in a filename
32
b) ?
33
a) Matches a single character from a set of characters
34
a) ls *.txt
35
a) Files like file1.txt, fileA.txt
36
a) Files that start with one character followed by .txt
37
a) Files that match either a, b, or c
38
a) file?.txt
39
a) Lists all .txt files with a lowercase letter at the end