Master the art of Working with Files Programmatically in this set of 30 MCQs. Covering topics such as file descriptors, redirection, file reading and writing, file search, and archiving and compression.
MCQs on Working with Files Programmatically
File Descriptors and Redirection
What is a file descriptor in Linux? a) A unique identifier for an open file b) A type of file extension c) A command to read files d) A command used to create files
Which file descriptor is used for standard output (stdout)? a) 1 b) 0 c) 2 d) 3
Which of the following is the correct syntax for redirecting output to a file? a) command > filename b) command >> filename c) command < filename d) Both a and b
How do you redirect both stdout and stderr to a file in Linux? a) command > file 2>&1 b) command >> file 2>&1 c) command 2>&1 > file d) command file > 2>&1
What does the 2> operator do in shell scripting? a) Redirects standard error (stderr) to a file b) Redirects standard output (stdout) to a file c) Appends output to a file d) Redirects both stdout and stderr to a file
In the context of file redirection, what does >& represent? a) Redirects standard output to standard error b) Combines both stdout and stderr into one stream c) Redirects both stdout and stderr to a file d) Changes the file descriptor of the output
Which of the following commands will append output to an existing file? a) command >> filename b) command > filename c) command << filename d) command <<< filename
How do you redirect the output of a command to both a file and the terminal simultaneously? a) command | tee filename b) command > filename | tee c) command 2>&1 | tee filename d) command >> filename | tee
What is the purpose of stdin in file handling? a) To accept input from the user or file b) To redirect output to a file c) To send output to the terminal d) To redirect both output and error
Which file descriptor is used for standard input (stdin)? a) 0 b) 1 c) 2 d) 3
Reading and Writing to Files
Which of the following commands will display the contents of a file? a) cat filename b) ls filename c) touch filename d) echo filename
How can you read the contents of a file line by line in shell scripting? a) while read line; do echo $line; done < filename b) cat filename c) tail filename d) head filename
What command is used to write the output of a command to a file in Linux? a) echo > filename b) echo “text” > filename c) cat filename > output d) None of the above
Which command allows you to append content to a file in Linux? a) echo “text” >> filename b) echo “text” > filename c) write filename d) cat >> filename
How do you read a file into a variable in a shell script? a) variable=$(<filename) b) variable=cat filename c) variable=read filename d) variable=echo filename
How can you output the content of a file without displaying extra newlines? a) cat -n filename b) cat -A filename c) cat filename d) cat -s filename
What command can be used to edit a file interactively in the terminal? a) nano filename b) vim filename c) vi filename d) All of the above
Which command would you use to overwrite the contents of a file? a) echo “New text” > filename b) echo “New text” >> filename c) cat > filename d) cat >> filename
What does the tee command do in Linux? a) Reads from standard input and writes to both standard output and a file b) Reads from a file and writes to standard output c) Deletes a file d) Appends to a file without modifying the original
How can you read the first 10 lines of a file? a) head -n 10 filename b) tail -n 10 filename c) cat -n filename d) head filename
Finding Files with find and locate
Which command can be used to find files in a directory recursively? a) locate filename b) find filename c) grep filename d) ls filename
What is the find command syntax to search for files by name? a) find /path -name filename b) find /path filename c) find filename d) locate filename
How would you use find to search for files modified in the last 7 days? a) find /path -mtime -7 b) find /path -atime -7 c) find /path -ctime -7 d) find /path -newermt 7
What does the locate command do? a) Searches for files based on a pre-built database b) Searches for files in the current directory c) Finds files based on file contents d) Lists files in a specified directory
How do you search for a file by its type using the find command? a) find /path -type f b) find /path -type d c) find /path -name filename -type f d) Both a and c
How do you find all files in a directory modified in the last 24 hours? a) find /path -mtime -1 b) find /path -newermt 24h c) locate /path d) find /path -mtime +1
Which find option allows you to find files with specific permissions? a) -perm b) -exec c) -name d) -size
Which command can you use to locate files in Linux if the database is up-to-date? a) locate filename b) find filename c) ls filename d) search filename
What does the -exec option do with the find command? a) Executes a command on the found files b) Prints the name of the files c) Modifies the contents of the files d) Deletes the files
Which command can you use to find empty files in a directory? a) find /path -empty b) find /path -size 0 c) find /path -type f -empty d) Both a and b
Answer Key
Qno
Answer (Option with Text)
1
a) A unique identifier for an open file
2
a) 1
3
d) Both a and b
4
a) command > file 2>&1
5
a) Redirects standard error (stderr) to a file
6
b) Combines both stdout and stderr into one stream
7
a) command >> filename
8
a) command
9
a) To accept input from the user or file
10
a) 0
11
a) cat filename
12
a) while read line; do echo $line; done < filename
13
b) echo “text” > filename
14
a) echo “text” >> filename
15
a) variable=$(<filename)
16
d) cat -s filename
17
d) All of the above
18
a) echo “New text” > filename
19
a) Reads from standard input and writes to both standard output and a file
20
a) head -n 10 filename
21
b) find filename
22
a) find /path -name filename
23
a) find /path -mtime -7
24
a) Searches for files based on a pre-built database