MCQs on Working with Files Programmatically | Shell Scripting

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

  1. 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
  2. Which file descriptor is used for standard output (stdout)?
    a) 1
    b) 0
    c) 2
    d) 3
  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
  4. 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
  5. 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
  6. 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
  7. Which of the following commands will append output to an existing file?
    a) command >> filename
    b) command > filename
    c) command << filename
    d) command <<< filename
  8. 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
  9. 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
  10. Which file descriptor is used for standard input (stdin)?
    a) 0
    b) 1
    c) 2
    d) 3

Reading and Writing to Files

  1. Which of the following commands will display the contents of a file?
    a) cat filename
    b) ls filename
    c) touch filename
    d) echo filename
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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

  1. Which command can be used to find files in a directory recursively?
    a) locate filename
    b) find filename
    c) grep filename
    d) ls filename
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. Which find option allows you to find files with specific permissions?
    a) -perm
    b) -exec
    c) -name
    d) -size
  8. 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
  9. 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
  10. 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

QnoAnswer (Option with Text)
1a) A unique identifier for an open file
2a) 1
3d) Both a and b
4a) command > file 2>&1
5a) Redirects standard error (stderr) to a file
6b) Combines both stdout and stderr into one stream
7a) command >> filename
8a) command
9a) To accept input from the user or file
10a) 0
11a) cat filename
12a) while read line; do echo $line; done < filename
13b) echo “text” > filename
14a) echo “text” >> filename
15a) variable=$(<filename)
16d) cat -s filename
17d) All of the above
18a) echo “New text” > filename
19a) Reads from standard input and writes to both standard output and a file
20a) head -n 10 filename
21b) find filename
22a) find /path -name filename
23a) find /path -mtime -7
24a) Searches for files based on a pre-built database
25d) Both a and c
26a) find /path -mtime -1
27a) -perm
28a) locate filename
29a) Executes a command on the found files
30d) Both a and b

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