Courseiva

CCNA The Power OF The Command Line Questions

59 questions · The Power OF The Command Line · All types, answers revealed

1
MCQmedium

A script named deploy.sh must run only when the preceding build command finishes without error. Which shell operator should you place between the two commands to achieve this?

A.||
B.|
C.&&
D.;
AnswerC

The && operator runs the right-hand command only if the left-hand command exits with status 0, which signals success. That matches the requirement exactly: deploy.sh runs only when the build succeeded. If the build fails, the shell skips deploy.sh and the overall exit status reflects the failure, which is useful in scripts.

Why this answer

Exit statuses drive shell control flow: zero means success, non-zero means failure. The && operator performs a logical AND on those statuses, executing its right operand only after a successful left operand. Chaining the build command with deploy.sh through && therefore guarantees deployment happens solely on a clean build, which is the standard idiom in shell scripts.

Exam trap

The trap here is treating the semicolon as a conditional separator, when it merely sequences commands and ignores whether the first one succeeded.

2
Multi-Selectmedium

Which TWO of the following commands can be used to view the contents of a file named 'data.txt'? (Choose two.)

Select 2 answers
A.wc data.txt
B.cat data.txt
C.less data.txt
D.grep data.txt
E.echo data.txt
AnswersB, C

The cat command concatenates and displays the entire contents of a file to standard output. It is a simple and common way to view file contents quickly, especially for short files. It directly shows the file's content without pagination or additional processing.

Why this answer

The cat and less commands both display the contents of a file. cat outputs the entire file at once, while less provides a paginated view. The other commands either search for patterns, count statistics, or print a literal string, none of which show the file's contents.

Exam trap

The trap here is assuming that any command that takes a filename as an argument will display the file's contents, such as grep or wc.

3
MCQmedium

You need to determine the absolute path of the `python3` executable that would run if you typed `python3` at the prompt. Which command displays this path?

A.whereis python3
B.find / -name python3
C.locate python3
D.which python3
AnswerD

The which command searches the directories in the PATH environment variable and prints the full path of the first matching executable. This directly answers the question of which python3 would run. It is the standard tool for locating a command's executable in the current shell environment.

Why this answer

The which command resolves a command name using the PATH variable and prints the first executable found. whereis, locate, and find do not determine execution order based on PATH.

Exam trap

The trap here is assuming that whereis or locate show the executable that would run, but they do not consider PATH order.

4
MCQeasy

A user wants to display the current working directory in the shell. Which command should they use?

A.pwd
B.dir
C.cd
D.ls
AnswerA

The pwd command stands for 'print working directory' and outputs the absolute pathname of the current directory. It is a shell built-in in most shells and directly fulfills the requirement to display the current working directory.

Why this answer

The pwd command is specifically designed to print the absolute path of the current working directory. Other commands like cd change directories, ls lists contents, and dir is an alternative listing command. Only pwd directly answers the question of where the user currently is in the filesystem hierarchy.

Exam trap

The trap here is confusing commands that list directory contents with the command that reveals the current directory's path.

5
MCQmedium

You want to find lines in 'file.txt' that do NOT contain the word 'success'. Which command works?

A.grep -n 'success' file.txt
B.grep -v 'success' file.txt
C.grep -c 'success' file.txt
D.grep -i 'success' file.txt
AnswerB

The -v flag inverts grep's matching logic, printing only lines that fail to match the pattern. Applied to 'success' in file.txt, it returns every line not containing that word, precisely satisfying the requirement to exclude matching lines rather than select them.

Why this answer

The `-v` option inverts the match, so `grep -v 'success' file.txt` prints every line that does NOT contain the pattern 'success'. This is the standard way to filter out matching lines in Linux/Unix. The other options either show matching lines, count them, or ignore case, but none exclude them.

Exam trap

The trap here is confusing grep's output modes: candidates often think `-c` shows lines (it only counts) or that `-i` excludes case variants (it includes them), while `-v` is the only option that inverts the match to show non-matching lines.

How to eliminate wrong answers

Option A is wrong because `grep -n 'success' file.txt` prints lines that DO contain 'success' and prefixes them with line numbers; it does not exclude them. Option C is wrong because `grep -c 'success' file.txt` only counts the number of matching lines and outputs a single number, not the lines themselves. Option D is wrong because `grep -i 'success' file.txt` performs a case-insensitive search for 'success', still showing matching lines (e.g., 'Success', 'SUCCESS') rather than excluding them.

6
MCQmedium

What is the result of 'echo $USER'?

A.It prints the current username.
B.It prints the path to the user home.
C.It returns an error.
D.It prints '$USER'.
AnswerA

The shell expands $USER to the value of the USER environment variable, which holds the login name of the account running the session. echo then writes that string to standard output, printing the current username.

Why this answer

$USER is a shell environment variable that holds the login name of the current user, so 'echo $USER' prints that username (e.g., 'alice'). The shell expands the variable before passing the argument to echo, so the literal string '$USER' is never printed.

Exam trap

The trap is confusing $USER with $HOME or thinking the shell prints the literal '$USER' — candidates must remember that unquoted variables are expanded by the shell before the command runs.

How to eliminate wrong answers

Option B is wrong because the home directory path is stored in $HOME, not $USER — a common confusion between the two variables. Option C is wrong because $USER is set by login shells (via PAM or /etc/profile) and echo of an unset variable would simply print an empty line, not an error; in practice $USER is always defined for interactive sessions. Option D is wrong because single quotes prevent expansion, but the question uses no quotes — the shell expands $USER, so the literal '$USER' is not printed.

7
MCQeasy

A developer is exploring a new project directory and wants to see the names of all files and directories, including those that start with a dot (hidden files). Which command should be used?

A.ls -h
B.ls -a
C.ls -l
D.ls -R
AnswerB

The -a option tells ls to list all entries, including those whose names begin with a dot (hidden files). By default, ls omits such files. Using ls -a in the project directory will display every file and directory, meeting the developer's need to see hidden items as well as regular ones.

Why this answer

The ls command lists directory contents, but by default it hides files whose names begin with a dot. The -a option overrides this and shows all entries, including hidden files. The other options modify output format or recursion but do not reveal hidden files, so they do not meet the stated requirement.

Exam trap

The trap here is assuming that any ls option will show hidden files, when only -a (or -A) includes dotfiles.

8
MCQmedium

A user wants to redirect the standard error of a command to a file named errors.txt while still displaying standard output on the terminal. Which redirection is correct?

A.command &> errors.txt
B.command 2> errors.txt
C.command 2>&1 > errors.txt
D.command > errors.txt
AnswerB

The notation 2> redirects file descriptor 2, which is standard error, to the specified file. Standard output (file descriptor 1) remains connected to the terminal, so normal output is still displayed. This exactly matches the requirement.

Why this answer

To redirect only standard error, use 2> filename. This leaves standard output untouched on the terminal. The > operator redirects only standard output, 2>&1 > file redirects error to the terminal and output to file, and &> redirects both streams to the file.

Exam trap

The trap here is confusing the order of redirections in 2>&1 > file, which does not achieve the same as 2> file.

9
MCQeasy

You need to display the contents of a file named 'config.txt' directly to your terminal screen. Which command should you use?

A.head config.txt
B.cat config.txt
C.echo config.txt
D.grep config.txt
AnswerB

cat reads the file sequentially and writes its contents to standard output, which is the terminal, satisfying the requirement to display config.txt directly on screen. It requires no pager or editor and outputs the raw file contents.

Why this answer

The `cat` command (concatenate) reads one or more files and writes their contents to standard output, which by default is the terminal. Running `cat config.txt` therefore prints the entire file to the screen in one pass, exactly matching the requirement to display file contents directly. It is the canonical tool for this purpose on Linux.

Exam trap

The trap is the word 'display' — candidates may pick head thinking it 'shows' the file, but head only shows the first 10 lines, and the question asks for the file's contents, which cat provides in full.

How to eliminate wrong answers

Option A is wrong because `head config.txt` displays only the first 10 lines by default, not the full file contents — it is a partial viewer, not a full display tool. Option C is wrong because `echo config.txt` simply prints the literal string 'config.txt' to the terminal; echo does not read files at all, it only outputs its arguments. Option D is wrong because `grep config.txt` treats 'config.txt' as the search pattern and waits for input from stdin (or errors), rather than displaying the file's contents.

10
Multi-Selectmedium

Which THREE of the following are valid ways to get help on a command?

Select 3 answers
A.help command
B.search command
C.info command
D.command --help
E.man command
AnswersC, D, E

The info command opens GNU Info documentation, a hypertext manual system providing detailed help for commands, satisfying the requirement for a valid help method. It reads .info files, typically installed under /usr/share/info, and offers navigation between nodes.

Why this answer

Option C (info command) is correct because GNU Info pages provide detailed, hyperlinked documentation for many commands and utilities, invoked as 'info <command>'. Option D (command --help) is correct because most GNU/Linux commands support the '--help' flag, which prints a concise usage summary and available options directly to standard output. Option E (man command) is correct because the 'man' utility displays the system manual pages for a command, covering its syntax, options, and description.

Options A and B are not valid: 'help command' is not a general help mechanism (the 'help' builtin only works for shell builtins, not arbitrary commands), and 'search command' is not a help command at all.

Exam trap

The trap is accepting 'help command' as universally valid — it only works for shell builtins, not external commands, so candidates who overgeneralize pick it incorrectly.

11
Multi-Selecthard

Which TWO of the following regex patterns are valid for grep to match the beginning or end of a line?

Select 2 answers
A.$
B.\
C..
D.*
E.^
AnswersA, E

End of line.

Why this answer

Option E, the caret (^), is correct because in grep's basic regular expressions it is the anchor that matches the start of a line, so a pattern like ^root matches lines beginning with 'root'. Option A, the dollar sign ($), is correct because it is the anchor that matches the end of a line, so a pattern like bash$ matches lines ending in 'bash'. Option C, the dot (.), is not an anchor; it matches any single character.

Option D, the asterisk (*), is a quantifier meaning zero or more repetitions of the preceding element, not a line anchor. Option B, the backslash (\), is an escape character used to remove the special meaning of the following character, not a beginning- or end-of-line anchor.

Exam trap

The trap here is confusing regex metacharacters with anchors — candidates often pick . or * because they look like 'special' regex symbols, forgetting that only ^ and $ denote line boundaries in grep.

12
Multi-Selecteasy

Which THREE of the following tools can be used for text processing?

Select 3 answers
A.awk
B.grep
C.sed
D.cat
E.ls
AnswersA, B, C

awk is a complete pattern-scanning and field-processing language: it splits each input record into fields, applies pattern-action rules and can reformat or summarise text. This satisfies the text-processing requirement alongside stream editors and search tools.

Why this answer

awk (A) is a full-featured text-processing language and utility that scans input line by line, splits fields, and performs pattern-action processing, making it a core text-processing tool. grep (B) is a text-processing tool because it searches input streams or files for lines matching a regular expression and prints the matching lines. sed (C) is a stream editor that performs text transformations such as substitution, deletion, and insertion on input streams, so it is also a text-processing tool. The unmarked options do not belong: cat (D) simply concatenates and outputs file contents without transforming or analyzing text, and ls (E) lists directory entries rather than processing text.

Exam trap

The trap is including `cat` as a text-processing tool because it is often used in pipelines — but cat only outputs data, it does not transform or filter it like awk, grep, or sed.

13
MCQhard

A colleague's shell script contains the line: find /var/log -name '*.log' -exec rm {} \; You need to explain what this line does before approving it. What is the effect of the -exec primary here?

A.It deletes each matching file one at a time by invoking rm once per file.
B.It prompts the user for confirmation before deleting each file.
C.It prints the names of matching files without deleting them.
D.It collects all matching files and passes them to a single rm invocation.
AnswerA

With -exec ... \;, find runs the specified command separately for every file that matches, substituting each path for the {} placeholder. The escaped semicolon terminates the command list for find rather than the shell. So each .log file under /var/log is passed individually to rm and removed, one invocation per match.

Why this answer

The -exec primary runs an external command for each file find locates, replacing the {} placeholder with the pathname. Terminating that command with an escaped semicolon makes find invoke it once per match, whereas a plus sign would batch multiple paths into fewer invocations. Understanding that terminator is essential before approving destructive one-liners in production scripts.

Exam trap

The trap here is overlooking the escaped semicolon terminator, which changes -exec from batching many files into one command to running the command separately for every match.

14
MCQeasy

Which command is used to display the current working directory in the terminal?

A.pwd
B.dir
C.cd
D.ls
AnswerA

pwd prints the absolute path of the directory the shell is currently in, reading it from the shell's tracked location rather than the filesystem. It directly answers the question of where you are, requiring no arguments.

Why this answer

The `pwd` command stands for 'print working directory' and is specifically designed to output the absolute path of the current directory in a Linux/Unix terminal. It reads the current directory from the kernel and displays it, making it the correct choice for this task. This is a fundamental command in the Linux Essentials (010-160) exam domain.

Exam trap

The trap here is confusing commands that list directory contents (`ls`, `dir`) with the command that displays the current directory path (`pwd`), or mixing up navigation (`cd`) with reporting (`pwd`).

How to eliminate wrong answers

Option B is wrong because `dir` is a command primarily used in Windows/DOS environments to list directory contents, not to display the current working directory; in Linux, it is often an alias for `ls` but does not show the path. Option C is wrong because `cd` (change directory) is used to navigate to a different directory, not to display the current one. Option D is wrong because `ls` lists the files and directories within the current directory, but does not print the working directory path itself.

15
MCQhard

A user runs the command `grep '^root' /etc/passwd` and sees only the line starting with 'root'. They now want to find all lines that do NOT start with 'root'. Which command should they use?

A.grep -i '^root' /etc/passwd
B.grep -v '^root' /etc/passwd
C.grep -n '^root' /etc/passwd
D.grep -c '^root' /etc/passwd
AnswerB

The -v option inverts the match, so grep prints lines that do not match the pattern. With the anchor ^root, it selects all lines that do not begin with 'root'. This is exactly what the user needs. The command is efficient and standard for excluding lines based on a pattern.

Why this answer

To invert a match in grep, the -v option is used. Combined with the anchored pattern ^root, it prints all lines that do not begin with 'root'. The other options either change case sensitivity, add line numbers, or count matches, but none of them invert the selection to exclude the matched lines.

Exam trap

The trap here is forgetting that grep's -v option inverts the match, or confusing it with -i for case-insensitivity.

16
Multi-Selecthard

Which TWO of the following commands can be used to redirect input from a file?

Select 2 answers
A.&&
B.cat file | command
C.<
D.>
E.>>
AnswersB, C

Piping cat's output into another command feeds that command's standard input from the file's contents, achieving input redirection indirectly. This satisfies the stem's requirement by supplying data via a pipe rather than the shell's < operator.

Why this answer

Option B (cat file | command) is correct because piping the output of cat file into another command feeds that file's contents to the command's standard input, effectively redirecting input from the file. Option C (<) is correct because the input redirection operator < explicitly redirects a command's standard input to come from a file, as in command < file. The unmarked options do not belong: && is a logical AND operator that conditionally chains commands based on exit status, while > and >> are output redirection operators that send standard output to a file (truncating and appending, respectively), not input.

Exam trap

The trap is mixing up input and output redirection operators — candidates often pick `>` or `>>` because they remember 'redirection' but forget those are for STDOUT, while `<` is the input operator.

17
Multi-Selecteasy

Which TWO of the following are commands to delete files or directories?

Select 2 answers
A.rmdir
B.rm
C.ls
D.cp
E.mv
AnswersA, B

`rmdir` removes empty directories, satisfying the stem's requirement for a deletion command. Unlike `rm -r`, which recurses through populated trees, `rmdir` refuses non-empty directories, so it deletes only the directory entry itself. This precise, non-recursive behaviour makes it one of the two valid deletion commands listed.

Why this answer

Option A, rmdir, is correct because it is the standard Linux/Unix command used to remove empty directories from the filesystem. Option B, rm, is correct because it is the standard command for deleting files and, with the -r (recursive) flag, directories and their contents. The other options do not delete anything: ls lists directory contents, cp copies files or directories, and mv moves or renames files or directories without removing them.

Exam trap

The trap is that candidates may include mv because it can 'remove' a file from its original location, but mv relocates rather than deletes — the exam expects only true deletion commands.

18
MCQeasy

A user is at the shell prompt and wants to display the current working directory. Which command should be used?

A.pwd
B.dir
C.ls
D.cd
AnswerA

pwd stands for 'print working directory' and outputs the absolute path of the current directory. It is a shell built-in in most shells, so it reflects the shell's internal notion of the current directory, which may differ from the kernel's if symbolic links are involved.

Why this answer

The pwd command prints the absolute pathname of the current working directory. Other commands like cd change directories, ls lists contents, and dir is not the standard tool for this purpose. pwd is a fundamental command for orientation in the filesystem.

Exam trap

The trap here is confusing listing directory contents with displaying the directory's path; ls shows files, while pwd shows where you are.

19
MCQeasy

While troubleshooting a service, a technician runs `ps aux` and sees a process in state `Z`. The process's parent is PID 1. Which statement best describes what the technician is observing?

A.The process is sleeping and is waiting for a signal to wake it.
B.The process is running in the foreground and has high CPU priority.
C.The process is stopped by a job control signal and can be resumed with `fg`.
D.The process has terminated, but its exit status has not yet been read by its parent.
AnswerD

A zombie process has finished execution, but the parent has not yet called wait() to collect its exit status. The kernel keeps a minimal process table entry until the parent reaps it. Because the parent here is PID 1 (init/systemd), it normally reaps children quickly; a lingering zombie usually indicates a bug or a stuck reaping loop.

Why this answer

A process in state Z is a zombie: it has already terminated, but its parent has not yet collected its exit status. The kernel retains a small entry until the parent calls wait(). Since the parent is PID 1, which normally reaps promptly, a lingering zombie points to a reaping problem rather than a running or stopped process.

Exam trap

The trap here is assuming that a Z state means the process is still running or merely paused, when it actually indicates an exited process awaiting reaping.

20
MCQeasy

Which symbol represents the current user's home directory in the shell?

A..
B.~
C...
D.$
AnswerB

The tilde character is expanded by the shell to the absolute path of the invoking user's home directory, so ~/file resolves correctly regardless of username. This satisfies the requirement for a portable shorthand referencing the current user's home.

Why this answer

The tilde symbol (~) is a shell metacharacter that expands to the absolute path of the current user's home directory (e.g., /home/username on Linux or /Users/username on macOS). It is recognized by the shell before command execution, so commands like cd ~ or ls ~/Documents work regardless of the current working directory. This is a standard feature in POSIX shells like Bash, Zsh, and Dash.

Exam trap

The trap here is confusing the single dot (.) with the tilde (~), as both are shorthand notations in the shell; candidates often mistakenly think . represents the home directory because it's used in relative paths like ./script, but it actually means the current directory.

How to eliminate wrong answers

Option A is wrong because a single dot (.) represents the current working directory, not the home directory. Option C is wrong because two dots (..) represent the parent directory of the current working directory. Option D is wrong because the dollar sign ($) is used to denote a shell variable (e.g., $HOME) or the command prompt for regular users, but it does not directly represent the home directory.

21
MCQmedium

You need to change the environment variable PATH to include the directory '/opt/bin'. Which command is standard?

A.set PATH=$PATH:/opt/bin
B.env PATH=$PATH:/opt/bin
C.export PATH=$PATH:/opt/bin
D.PATH=$PATH:/opt/bin
AnswerC

The export builtin marks PATH for export to child processes, and $PATH:/opt/bin appends the new directory while preserving existing entries. This is the standard POSIX shell syntax for persistently modifying PATH within the current session, matching the stem's requirement to include /opt/bin.

Why this answer

The export command marks a shell variable for inclusion in the environment of subsequently executed child processes, so 'export PATH=$PATH:/opt/bin' both appends /opt/bin to PATH and ensures the change propagates to child processes. This is the standard, portable way to modify PATH in bash and other POSIX shells. Without export, the change would remain local to the current shell.

Exam trap

The trap is forgetting that assignment alone (PATH=...) does not export the variable — candidates must remember that export is required for child processes to see the change.

How to eliminate wrong answers

Option A is wrong because 'set' in bash sets shell options or positional parameters, not environment variables — 'set PATH=...' does not export PATH and behaves differently across shells (in some it merely sets a shell variable). Option B is wrong because 'env PATH=$PATH:/opt/bin' runs a command named PATH=... in a modified environment; it does not persist the change in the current shell and would fail as a command invocation. Option D is wrong because 'PATH=$PATH:/opt/bin' assigns the variable in the current shell but does not export it, so child processes (including newly launched commands) would not inherit the updated PATH.

22
MCQeasy

You want to find all occurrences of the string 'ERROR' within the 'syslog' file. Which command is most appropriate?

A.cat 'ERROR' syslog
B.find 'ERROR' syslog
C.grep 'ERROR' syslog
D.sed 'ERROR' syslog
AnswerC

grep searches file contents line by line for a pattern, printing matching lines. Given the constraint of locating every 'ERROR' occurrence inside the syslog file, grep 'ERROR' syslog is the direct tool; alternatives like find or locate operate on filenames, not file contents.

Why this answer

'grep' (global regular expression print) searches a file or input stream for lines matching a pattern and prints them. Running 'grep ERROR syslog' scans the syslog file and outputs every line containing 'ERROR', which is exactly the task described.

Exam trap

The trap is that 'find' and 'grep' are both search commands, but 'find' searches the filesystem for files while 'grep' searches file contents for text — candidates who conflate the two pick 'find' for a content-search task.

How to eliminate wrong answers

Option A is wrong because 'cat' concatenates and prints entire files — 'cat ERROR syslog' would try to open a file literally named 'ERROR' and then print syslog in full, not filter it. Option B is wrong because 'find' searches the filesystem for files matching criteria like name or size; it does not search file contents for a string. Option D is wrong because 'sed' is a stream editor used for substitution, deletion, and transformation — 'sed ERROR syslog' would treat 'ERROR' as a sed script and likely error out, not search for the string.

23
MCQmedium

A user wants to run a command that will continue running even after the terminal is closed. Which command should be used to achieve this?

A.nohup ./long_script.sh &
B.screen ./long_script.sh
C.bg ./long_script.sh
D.disown ./long_script.sh
AnswerA

The nohup command makes a process immune to hangup signals, so it continues running after the terminal closes. The ampersand (&) runs it in the background. This combination ensures the script persists beyond the session, which is exactly what the user needs.

Why this answer

To keep a process running after the terminal closes, use nohup to ignore hangup signals and & to run it in the background. This detaches the process from the terminal's session. The other commands either do not start a new process, require a different syntax, or do not prevent termination on terminal close.

Exam trap

The trap here is confusing job control commands like bg and disown with process detachment tools, or misusing screen syntax.

24
MCQhard

A user wants to create a compressed archive of the directory /home/user/project, including all its subdirectories, and name the archive project.tar.gz. Which command should be used?

A.zip -r project.tar.gz /home/user/project
B.tar -czf project.tar.gz /home/user/project
C.gzip -r /home/user/project
D.tar -xzf project.tar.gz /home/user/project
AnswerB

The tar command with -c creates a new archive, -z compresses it with gzip, and -f specifies the archive filename. Including the directory path archives the entire directory tree. This command correctly creates project.tar.gz containing the project directory and its contents.

Why this answer

To create a gzip-compressed tar archive, the tar command with -c, -z, and -f options is used. The -c flag creates a new archive, -z applies gzip compression, and -f specifies the output filename. Providing the directory path archives the entire directory tree.

Other commands either extract, compress individual files, or create a different archive format.

Exam trap

The trap here is confusing the -c (create) and -x (extract) options of tar, or assuming that zip with a .tar.gz name produces a tar.gz file.

25
Multi-Selectmedium

You are reviewing a shell script and need to identify commands that are handled internally by the shell rather than by launching a separate executable. Which two of the following are shell builtins? (Choose two.)

Select 2 answers
A.cd
B.tar
C.grep
D.export
E.ls
AnswersA, D

Changing the current working directory affects the shell process itself, so cd must be implemented as a builtin. A separate executable could change only its own working directory and then exit, leaving the parent shell unchanged. That is why cd appears in the shell's builtin list and cannot be replaced by an external binary in normal use.

Why this answer

Builtins execute inside the shell process because they must alter shell state or avoid process overhead. cd changes the working directory of the shell itself, and export modifies the environment inherited by child processes. Neither effect could persist if the command ran as a separate executable, which is why both are implemented internally alongside others such as echo, pwd, and read.

Exam trap

The trap here is assuming any frequently typed command must be a builtin, when commands like ls and grep are external binaries that merely happen to be convenient.

26
MCQmedium

A system administrator needs to change the permissions of a script named backup.sh so that the owner can read, write, and execute it, while group members and others can only read and execute it. Which command accomplishes this?

A.chmod 644 backup.sh
B.chmod 755 backup.sh
C.chmod 777 backup.sh
D.chmod 750 backup.sh
AnswerB

The numeric mode 755 sets owner permissions to read, write, and execute (7), and group and others to read and execute (5). This exactly matches the requirement: the owner gets full control, while group and others can read and execute but not write. It is a standard way to make a script executable for everyone while restricting write access.

Why this answer

The requirement is owner read/write/execute (7), group read/execute (5), others read/execute (5). Numeric mode 755 represents exactly that combination. The other modes either omit execute permissions, grant excessive write access, or remove permissions from others entirely, so they do not match the specified access policy.

Exam trap

The trap here is confusing the numeric values for read, write, and execute, or forgetting that the three digits correspond to owner, group, and others in that order.

27
Multi-Selectmedium

Which THREE of the following are common shell built-in command types?

Select 3 answers
A.echo
B.cd
C.grep
D.tar
E.export
AnswersA, B, E

echo is a shell built-in implemented within the shell itself rather than as a separate executable, so it runs without spawning a child process. This matches the stem's criterion for common shell built-in command types, alongside cd and export.

Why this answer

echo (A) is a shell built-in because most shells implement it internally to print arguments to standard output, and it is listed among the shell's built-in commands. cd (B) must be a built-in since changing the current working directory affects the shell process itself, and an external program could not alter its parent shell's directory. export (E) is a built-in because it sets or modifies environment variables in the current shell's environment, which external commands cannot do for the parent shell. grep (C) and tar (D) are external utilities located as separate executables (e.g., /usr/bin/grep, /bin/tar) rather than shell built-ins, so they do not belong.

Exam trap

The trap is assuming any command you type at the prompt is a built-in — candidates forget that grep and tar are external binaries, and that the defining test is whether the command must run inside the shell process to affect shell state.

28
Multi-Selecteasy

Which TWO of the following are valid commands for displaying file contents?

Select 2 answers
A.cat
B.touch
C.chmod
D.grep
E.less
AnswersA, E

`cat` concatenates and writes files to standard output, directly displaying a file's contents in the terminal. It satisfies the stem's requirement for a valid display command, and remains one of the two expected answers alongside `less`. Its core purpose is reading files sequentially, making it correct here.

Why this answer

Option A, cat, is correct because the cat command concatenates and prints file contents to standard output, making it a standard way to display a file's contents. Option E, less, is correct because less is a pager that displays file contents one screen at a time and allows scrolling through the file. Option B, touch, is not correct because touch creates empty files or updates file timestamps rather than displaying contents.

Option C, chmod, is not correct because chmod changes file permission modes and does not read or display file data. Option D, grep, is not correct because grep searches for and prints matching lines from input or files, which is a filtering operation rather than a general file-content display command.

Exam trap

The trap is confusing tools that manipulate files (touch, chmod) with tools that display contents, and mistaking grep (a search filter) for a general-purpose viewer.

29
Multi-Selecthard

Which TWO of the following signals can be used to interact with processes?

Select 2 answers
A.SIGTERM
B.SIGKILL
C.SIGSTOPPED
D.SIGEXIT
E.SIGRUN
AnswersA, B

SIGTERM requests graceful process termination, allowing the program to save state and release resources before exiting. It satisfies the stem's requirement for a signal used to interact with running processes, since the kernel delivers it to a target process, which may catch, ignore, or handle it rather than being forcibly killed.

Why this answer

SIGTERM (A) is a standard, catchable termination signal (signal number 15) that can be sent to a process with the kill command to request it to shut down gracefully, allowing it to run cleanup handlers before exiting. SIGKILL (B) is signal number 9, an uncatchable and unblockable signal that forcibly terminates a process immediately, making it the other valid way to interact with and control a running process. The remaining options are not real signals: SIGSTOPPED (C) is not a defined signal (the actual stop signal is SIGSTOP, number 19), SIGEXIT (D) does not exist in the standard signal set, and SIGRUN (E) is likewise not a valid signal name.

Exam trap

The trap is that the wrong options look like plausible signal names (SIGSTOPPED, SIGEXIT, SIGRUN) — candidates must recall the actual signal set and recognise that SIGSTOPPED is a state, not a signal.

30
Multi-Selecthard

Which THREE of the following are valid ways to run commands sequentially?

Select 3 answers
A.;
B.|
C.>
D.&&
E.||
AnswersA, D, E

The semicolon operator runs commands sequentially regardless of whether earlier commands succeed, satisfying the requirement for unconditional sequential execution. Unlike `&&`, which halts on failure, or `||`, which runs only after failure, `;` simply separates and orders commands, making it a valid mechanism for running them one after another.

Why this answer

Option A (;) is correct because the semicolon is the shell's unconditional command separator, so `cmd1 ; cmd2` runs cmd2 after cmd1 finishes regardless of cmd1's exit status. Option D (&&) is correct because the logical AND operator runs the second command only if the first command exits successfully (exit status 0), which is a standard way to chain commands sequentially with dependency. Option E (||) is correct because the logical OR operator runs the second command only if the first command fails (non-zero exit status), also executing commands one after another in sequence.

Option B (|) is not a sequential runner but a pipe that connects the standard output of one command to the standard input of another, and both processes typically run concurrently. Option C (>) is a redirection operator that sends standard output to a file, not a command-sequencing construct.

Exam trap

The trap here is confusing the pipe (|), which chains commands as a data pipeline, with the sequential control operators (;, &&, ||), causing candidates to select | as a way to run commands one after another.

31
MCQhard

You need to find files modified within the last 7 days. Which find flag is correct?

A.find . -ctime 7
B.find . -mtime -7
C.find . -mtime +7
D.find . -atime 7
AnswerB

The -mtime -7 predicate matches files whose data was modified less than seven days ago, satisfying the requirement to find recently modified files. The minus sign means "less than", whereas +7 would match files older than seven days.

Why this answer

The `-mtime -7` predicate matches files whose modification time is less than 7 days ago (i.e., within the last 7 days). The minus sign means 'less than N', so `-mtime -7` correctly captures recently modified files. This is the standard find syntax for time-based filtering and directly answers the requirement.

Exam trap

The trap is the sign convention — candidates often assume `-mtime 7` means 'within 7 days', but the bare number means exactly 7 days, the minus means less than, and the plus means more than, so only `-mtime -7` fits 'within the last 7 days'.

How to eliminate wrong answers

Option A is wrong because `-ctime 7` matches files whose inode change time is exactly 7 days old (24-hour periods), not files modified within the last 7 days — and ctime tracks metadata changes, not content modification. Option C is wrong because `-mtime +7` matches files modified *more* than 7 days ago, the opposite of what is needed. Option D is wrong because `-atime 7` matches files accessed exactly 7 days ago (access time), not modified within the last 7 days, and atime is often disabled via noatime mounts.

32
MCQhard

A junior administrator runs `grep -r "timeout" /etc` inside a Bash login shell. The output scrolls past the terminal window, so the administrator wants to view the complete results one screen at a time while also being able to scroll back through the earlier lines. Which command should the administrator have used instead?

A.grep -r "timeout" /etc &
B.grep -r "timeout" /etc | less
C.grep -r "timeout" /etc | tee
D.grep -r "timeout" /etc > /dev/null
AnswerB

Piping the recursive grep output into less launches a pager that displays one screenful at a time and lets the administrator scroll forward and backward through the entire result set. This directly satisfies both requirements: viewing the complete output screen by screen and being able to revisit earlier lines without rerunning the search.

Why this answer

Sending the recursive grep results through a pager is the standard way to review long output interactively. The less pager presents one screenful at a time and supports scrolling backward and forward, so the administrator can read every matched line and revisit earlier ones. The other choices either discard the output, duplicate the original scrolling behaviour, or detach the job without improving readability.

Exam trap

The trap here is assuming that any pipe changes how output is presented, when only a pager such as less actually buffers the results for interactive screen-by-screen viewing.

33
MCQhard

You want to execute two commands sequentially, but only if the first command succeeds. Which operator is used?

A.;
B.||
C.&&
D.|
AnswerC

The && operator runs the second command only when the first exits with status zero, giving conditional sequential execution. The semicolon runs commands regardless of outcome, while || executes the second only on failure, so && matches the success-dependent requirement.

Why this answer

The && operator is the logical AND control operator in Bash and other POSIX shells. It executes the command on the right only if the command on the left exits with a status of 0 (success). This is the standard way to chain commands with a success dependency, such as 'make && make install'.

Exam trap

The trap is confusing && with || or ; — candidates often remember '&& means and' but forget that it specifically gates execution on the success (exit code 0) of the preceding command.

How to eliminate wrong answers

Option A is wrong because the semicolon (;) executes commands sequentially regardless of whether the first command succeeds or fails — it is an unconditional separator. Option B is wrong because the || operator is logical OR and executes the right-hand command only if the left-hand command fails (non-zero exit status), which is the opposite of the requirement. Option D is wrong because the pipe (|) connects the standard output of the left command to the standard input of the right command, running them concurrently as a pipeline, not sequentially with a success condition.

34
MCQeasy

Which command removes a directory and all its contents recursively?

A.rm -rf dir
B.rm -d dir
C.del -r dir
D.rmdir dir
AnswerA

rm -rf combines recursive (-r) traversal of the directory tree with force (-f) suppression of prompts, deleting the directory and all contents. The -r flag satisfies the stem's recursive requirement, while -f avoids interactive confirmation for each entry.

Why this answer

rm -rf dir removes the directory and everything inside it recursively (-r) and forcibly (-f), without prompting. This is the standard command to delete a directory tree. The other options either don't exist, don't recurse, or only remove empty directories.

Exam trap

The trap is mixing Windows and Linux commands — candidates pick del -r (a Windows-ism) or rmdir, forgetting that only rm -rf performs recursive forced deletion on Linux.

How to eliminate wrong answers

Option B is wrong because rm -d attempts to remove an empty directory (equivalent to rmdir) and will fail if the directory contains files; it does not recurse. Option C is wrong because del is a Windows/DOS command, not a Linux command, and -r is not a valid del option in that context. Option D is wrong because rmdir only removes empty directories and errors out if contents exist, so it cannot remove a directory and all its contents.

35
Multi-Selecthard

Which TWO of the following help manage compressed archive contents?

Select 2 answers
A.find
B.cat
C.gzip
D.grep
E.tar
AnswersC, E

gzip compresses single files into .gz archives, satisfying the requirement to manage compressed archive contents. It reduces file size for storage or transfer, though it cannot bundle multiple files together—that needs tar first. For the exam's purpose, gzip is a recognised compression tool that directly handles archive content.

Why this answer

Option C, gzip, is correct because it is the standard GNU/Linux compression utility that compresses and decompresses files (creating .gz archives) and can also list compression information with gzip -l, directly managing compressed archive contents. Option E, tar, is correct because it is the archiving utility that bundles multiple files and directories into a single archive and, when combined with compression options such as -z (gzip), -j (bzip2), or -J (xz), creates and extracts compressed archives like .tar.gz, making it a primary tool for managing compressed archive contents. Option A, find, is incorrect because it searches the filesystem for files matching criteria such as name, size, or permissions; it does not compress, decompress, or manipulate archive contents.

Option B, cat, is incorrect because it concatenates and displays file contents to standard output and has no archive or compression management capability. Option D, grep, is incorrect because it searches text patterns within files and, while it can search inside uncompressed streams, it does not create, extract, or otherwise manage compressed archives.

Exam trap

The trap is conflating compression with archiving — candidates may pick find or grep because they associate them with file handling, missing that only gzip (compress) and tar (archive) manage archive contents.

36
MCQeasy

Which command-line shortcut allows you to autocomplete filenames and commands?

A.Enter
B.Shift + Insert
C.Tab
D.Ctrl + C
AnswerC

Pressing Tab invokes the shell's completion facility, which reads the current partial token and expands it against available commands or filenames in the search path. This satisfies the requirement for a keyboard shortcut that autocompletes both commands and filenames without executing anything.

Why this answer

Pressing the Tab key in a Linux shell triggers the readline completion feature, which autocompletes filenames, directory paths, and command names based on what you have typed so far. If multiple matches exist, pressing Tab twice lists all candidates. This is a core productivity shortcut in bash and most other shells.

Exam trap

The trap is confusing terminal editing shortcuts — candidates sometimes pick Ctrl+C thinking it 'completes' or cancels input, but it actually sends SIGINT to kill the running process.

How to eliminate wrong answers

Option A is wrong because Enter submits the current command line for execution — it does not perform completion. Option B is wrong because Shift+Insert is a terminal emulator shortcut for pasting text from the clipboard (the X11 primary selection), not for autocompletion. Option D is wrong because Ctrl+C sends SIGINT to interrupt and terminate the currently running foreground process — it aborts commands rather than completing them.

37
MCQmedium

If you want to view a long text file page by page, which command is most suitable?

A.tail filename
B.less filename
C.cat filename
D.grep filename
AnswerB

less opens the file in a pager, letting you scroll forward and backward through long content one screen at a time without loading everything into memory. It also supports searching, unlike cat, which dumps the entire file at once.

Why this answer

The less command is a pager that displays a file one screenful at a time, allowing the user to scroll forward and backward with arrow keys, Page Up/Down, and search with '/'. It reads the file lazily and does not load the entire content into memory, making it ideal for viewing long text files interactively. This is the canonical tool for page-by-page file inspection on Linux.

Exam trap

The trap is that cat and less both 'show' a file, but only less paginates — candidates who equate 'view a file' with cat pick the wrong tool, forgetting that cat floods the terminal.

How to eliminate wrong answers

Option A is wrong because tail only shows the last 10 lines by default (or a specified number with -n), not the whole file page by page. Option C is wrong because cat concatenates and dumps the entire file to standard output at once, scrolling past the terminal without pausing. Option D is wrong because grep searches for patterns within files and prints matching lines; it is not a pager and does not display files page by page.

38
MCQeasy

Which character is used to append output to an existing file without deleting its original content?

A.<
B.|
C.>
D.>>
AnswerD

The double chevron opens the target file in append mode, positioning the write offset at end-of-file so existing content survives. A single chevron would truncate instead, so >> satisfies the requirement to preserve original content.

Why this answer

The >> operator redirects standard output and appends it to the end of an existing file, preserving the original content. This is the standard shell redirection for adding data without truncation. The single > would overwrite the file, and the other symbols perform input redirection or piping, not appending.

Exam trap

The trap is confusing > with >> — candidates who know redirection exists but forget that a single > truncates the file, destroying existing content.

How to eliminate wrong answers

Option A is wrong because < is the input redirection operator, feeding a file's contents as standard input to a command, not writing output. Option B is wrong because | is the pipe operator, which sends the standard output of one command to the standard input of another, not to a file. Option C is wrong because > redirects output but truncates/overwrites the target file, destroying existing content — the opposite of appending.

39
MCQmedium

A teammate wants to search a large text file for lines containing the word 'error', ignoring case, and display the matching line numbers. Which command accomplishes this?

A.grep -l error filename.txt
B.grep -v error filename.txt
C.grep -in error filename.txt
D.grep -c error filename.txt
AnswerC

The -i flag makes matching case-insensitive, and -n prefixes each matching line with its line number. Combined, they satisfy both requirements in one pass over the file. Without -i, only lowercase occurrences would match; without -n, no line numbers would appear, so both flags are needed together.

Why this answer

grep options compose to refine output: -i removes case sensitivity so Error and ERROR match, while -n annotates each result with its line number in the file. Supplying both flags in a single invocation produces exactly the requested listing. Other flags such as -c, -v, and -l change the output in ways that do not satisfy this scenario.

Exam trap

The trap here is selecting a count or file-list flag when the requirement is to display the matching lines themselves with their positions in the file.

40
MCQmedium

You need to set an environment variable named 'MYVAR' to 'hello' so that it is available to child processes. Which command is correct?

A.set MYVAR=hello
B.MYVAR=hello
C.env MYVAR=hello
D.export MYVAR=hello
AnswerD

The export builtin marks MYVAR for inclusion in the environment of subsequently executed commands, so child processes inherit it. Assigning without export would create only a shell variable, invisible to children, failing the stem's inheritance requirement.

Why this answer

The export builtin marks a shell variable for inclusion in the environment of subsequently executed child processes. Writing export MYVAR=hello both assigns the value and exports it in one step, making it visible to programs launched from that shell. Without export, the variable would remain a shell-local variable and would not be inherited by child processes.

Exam trap

The trap is treating plain assignment (MYVAR=hello) as sufficient for child-process visibility; candidates forget that export is what promotes a shell variable into the inherited environment.

How to eliminate wrong answers

Option A is wrong because set MYVAR=hello is not valid Bash syntax for assignment; set is used to set shell options or positional parameters, and this form would not create the variable correctly. Option B is wrong because MYVAR=hello alone creates a shell variable but does not export it, so child processes would not see it. Option C is wrong because env MYVAR=hello runs the command 'MYVAR=hello' as a program (which does not exist) rather than setting a persistent variable; env is used to run a command with a modified environment, not to define a variable for the current shell.

41
MCQmedium

You wish to send the output of the 'ls -l' command directly into a file named 'dirlist.txt', overwriting any existing content. Which operator should you use?

A.ls -l > dirlist.txt
B.ls -l | dirlist.txt
C.ls -l >> dirlist.txt
D.ls -l < dirlist.txt
AnswerA

The > redirection operator sends stdout to dirlist.txt, truncating and overwriting any existing content, exactly as the scenario requires. The shell opens the file for writing before executing ls, so prior contents are discarded rather than appended.

Why this answer

The single greater-than operator (>) redirects standard output to a file, creating the file if it does not exist or truncating and overwriting it if it does. Therefore 'ls -l > dirlist.txt' writes the directory listing into dirlist.txt, replacing any prior content. This is the standard POSIX shell redirection for overwrite semantics.

Exam trap

The trap is confusing > (overwrite) with >> (append), or mixing up input (<) and output (>) redirection — candidates often pick >> thinking it 'sends' output when it actually appends.

How to eliminate wrong answers

Option B is wrong because the pipe (|) connects the stdout of ls -l to the stdin of another command, but 'dirlist.txt' is a filename, not an executable, so the shell would attempt to run it as a command and fail. Option C is wrong because >> is the append operator, which adds to the end of the file rather than overwriting existing content as the question requires. Option D is wrong because < is the input redirection operator, which feeds dirlist.txt as stdin to ls -l — the opposite direction of what is needed, and ls does not read filenames from stdin anyway.

42
MCQeasy

Which command is used to display lines of a file that match a pattern, while ignoring case?

A.grep -v
B.grep -w
C.grep -r
D.grep -i
AnswerD

The -i flag makes grep perform case-insensitive matching, so patterns match regardless of upper or lower case letters. Combined with grep's default line-matching behaviour, it displays every line containing the pattern without regard to casing.

Why this answer

The 'grep -i' option performs a case-insensitive pattern match, so lines containing 'Error', 'error', or 'ERROR' all match a search for 'error'. This is the standard way to search files while ignoring letter case. The -i flag is documented in the grep man page as 'ignore case distinctions in patterns and input data'.

Exam trap

010-160 often tests the difference between grep flags — candidates confuse -i (ignore case) with -v (invert), -w (whole word), and -r (recursive), so they must map each flag to its exact behavior.

How to eliminate wrong answers

Option A is wrong because 'grep -v' inverts the match, printing lines that do NOT contain the pattern — the opposite of what is needed. Option B is wrong because 'grep -w' matches only whole words, ensuring the pattern is bounded by non-word characters; it does not affect case sensitivity. Option C is wrong because 'grep -r' performs a recursive search through directories, which changes the scope of the search, not the case handling.

43
MCQmedium

You need to create a compressed archive of the '/home/user/docs' directory named 'docs.tar.gz'. Which flag ensures gzip compression is applied?

A.tar -czf docs.tar.gz /home/user/docs
B.tar -jf docs.tar.gz /home/user/docs
C.tar -xzf docs.tar.gz /home/user/docs
D.tar -cf docs.tar.gz /home/user/docs
AnswerA

The `-z` flag invokes gzip compression during archive creation, satisfying the requirement for a `.tar.gz` file. Combined with `-c` to create and `-f` to specify the filename, `tar -czf` writes the compressed archive directly, avoiding a separate compression step.

Why this answer

The `-czf` flag combination in `tar` specifies: `-c` to create a new archive, `-z` to filter the archive through gzip for compression, and `-f` to specify the archive filename. This directly produces a gzip-compressed tarball named `docs.tar.gz` from the `/home/user/docs` directory. The `-z` flag is the key to invoking gzip compression, which is exactly what the question asks for.

Exam trap

The trap here is confusing the compression flags: candidates often mix up `-z` (gzip), `-j` (bzip2), and `-J` (xz), or mistakenly use `-x` (extract) instead of `-c` (create), leading to wrong answers like B or C.

How to eliminate wrong answers

Option B is wrong because `-j` invokes bzip2 compression, not gzip, which would produce a `.tar.bz2` file and is not the requested format. Option C is wrong because `-x` is used to extract an archive, not create one, so it would attempt to extract from `docs.tar.gz` rather than create it. Option D is wrong because it lacks any compression flag (`-z`, `-j`, etc.), so it would create an uncompressed tar archive named `docs.tar.gz` (misleadingly), not a gzip-compressed one.

44
MCQmedium

A user wants to find all files under /home that are larger than 100 MB and were modified in the last 7 days. Which command accomplishes this?

A.find /home -type f -size 100M -mtime 7
B.find /home -type f -size +100M -mtime -7
C.find /home -type f -size +100M -mtime +7
D.find /home -name '*' -size +100M -mtime +7
AnswerB

This find command searches /home, restricts to regular files with -type f, matches files larger than 100 MB with -size +100M, and files modified within the last 7 days with -mtime -7. All conditions are ANDed by default, so it returns exactly the desired files.

Why this answer

The correct command uses -type f for regular files, -size +100M for files larger than 100 MB, and -mtime -7 for files modified within the last 7 days. The other options either match exact sizes, exact ages, or use +7 which selects older files, failing the scenario's criteria.

Exam trap

The trap here is misreading the plus and minus signs in find: +100M means larger, -7 means within the last 7 days, and reversing them changes the result.

45
MCQmedium

What is the result of the command 'ls -la | grep "^d"'?

A.It shows only hidden files.
B.It lists files that contain the letter 'd'.
C.It deletes all directories.
D.It lists only directories in the current directory.
AnswerD

grep "^d" matches lines beginning with d, the first character in ls -la output denoting a directory. Piping the long listing through this filter therefore returns only directories, satisfying the scenario's requirement to list directories alone.

Why this answer

The command 'ls -la | grep "^d"' lists all files and directories in long format, including hidden ones, and then filters the output to show only lines starting with 'd', which indicates a directory. Thus, it lists only directories in the current directory.

Exam trap

Linux Essentials often tests the interpretation of command pipelines; candidates may think grep searches for the letter 'd' anywhere, but the '^' anchors it to the start of the line.

How to eliminate wrong answers

Option A is wrong because hidden files are included but not exclusively; the command shows directories, hidden or not. Option B is wrong because grep searches for lines starting with 'd', not files containing the letter 'd'. Option C is wrong because the command does not delete anything; it only lists.

46
MCQmedium

A user wants to find all files under /var/log that have been modified within the last 24 hours and are larger than 1 MB, then save the list to /tmp/recent_large.txt. Which command accomplishes this?

A.ls -lR /var/log | awk '$6 > 1 && $7 > 1' > /tmp/recent_large.txt
B.locate -mtime -1 -size +1M /var/log > /tmp/recent_large.txt
C.find /var/log -type f -mtime -1 -size +1M > /tmp/recent_large.txt
D.grep -R -l --include='*' -mtime -1 -size +1M /var/log > /tmp/recent_large.txt
AnswerC

This command uses find with -mtime -1 to match files modified less than 24 hours ago, -size +1M for files larger than 1 MB, and redirects the output to the specified file. It directly meets all criteria: search path, file type, time, size, and output redirection.

Why this answer

The find command is designed to search for files based on metadata such as modification time and size. Using -mtime -1 selects files modified within the last 24 hours, and -size +1M selects files larger than 1 MB. Redirection saves the output.

The other commands lack these filtering capabilities or misinterpret the options.

Exam trap

The trap here is confusing grep's recursive search with find's metadata filtering, assuming grep can filter by time or size.

47
MCQmedium

While troubleshooting a script, you run `grep -n 'error' /var/log/app.log` and see output lines prefixed with numbers. A colleague asks what those numbers represent. What do they indicate?

A.The total count of matching lines found in the file.
B.The line number in the file where each matching line occurs.
C.The process ID of the application that wrote each log entry.
D.The byte offset from the start of the file to each match.
AnswerB

The -n option to grep prefixes each matching line with its 1-based line number within the input file. This is useful for locating matches in large files or when you need to reference a specific line later. In this scenario, the numbers identify where in /var/log/app.log each 'error' match appears.

Why this answer

The -n flag tells grep to prefix each matching line with its line number in the source file. This helps pinpoint where matches occur without opening an editor. Other grep flags like -c count matches and -b show byte offsets, but only -n provides the line position shown in the scenario.

Exam trap

The trap here is confusing grep's line-number prefix with a match count or byte offset, which are produced by different flags.

48
MCQmedium

A user needs to find every regular file under /var/log whose name ends in `.log` and whose size is larger than 10 megabytes, then delete them. Which command accomplishes this safely?

A.find /var/log -type f -name '*.log' -size +10M -delete
B.ls -l /var/log/*.log | awk '$5>10485760 {print $9}' | rm
C.find /var/log -name '*.log' -size +10M -exec rm -rf {} \;
D.grep -rl '*.log' /var/log | xargs rm -f
AnswerA

The find command combines the predicates -type f (regular files), -name '*.log' (name pattern), -size +10M (greater than 10 megabytes), and -delete to remove matches. This is safe because -delete only acts on files that satisfy all preceding tests, and it avoids invoking a separate process for each file. It also respects the directory traversal order.

Why this answer

The find command with -type f, -name '*.log', -size +10M, and -delete precisely targets regular files larger than 10 MB with the .log suffix and removes them. It avoids content-based matching, does not rely on parsing ls output, and does not risk recursive directory deletion, making it the safe and correct choice.

Exam trap

The trap here is confusing filename matching with content searching, and forgetting that rm does not read from standard input.

49
MCQeasy

A user wants to count the number of lines in a file named 'access.log'. Which command should be used?

A.head -n 999999 access.log | wc -l
B.wc -l access.log
C.cat -n access.log
D.grep -c '' access.log
AnswerB

The wc command with the -l option counts the number of newline characters in the file, which corresponds to the number of lines. This is the standard and simplest way to count lines in a file, directly providing the required information without additional processing.

Why this answer

The wc -l command is specifically designed to count lines in a file by counting newline characters. It is efficient, simple, and the de facto standard for this task. The other commands either display the file, use a less direct method, or introduce unnecessary complexity and potential inaccuracies.

Exam trap

The trap here is overcomplicating a simple task by using grep or head pipelines instead of the purpose-built wc command.

50
MCQhard

A user runs `grep -E '^[A-Z][a-z]+ [0-9]{3}$' file.txt` and gets no output, even though file.txt contains lines like `Alpha 123` and `Beta 456`. What is the most likely explanation?

A.The pattern requires exactly one uppercase letter followed by one or more lowercase letters, then a space, then exactly three digits, and the end of line; the lines may contain trailing spaces or other characters.
B.The pattern requires the first letter to be uppercase and the rest lowercase, but the lines start with uppercase and have digits, which is not allowed by [a-z]+.
C.The pattern uses {3} which is not supported by grep without the -P option.
D.The -E option is invalid for grep and causes it to ignore the pattern.
AnswerA

The regular expression ^[A-Z][a-z]+ [0-9]{3}$ anchors the match to the entire line. The $ asserts the end of line immediately after the three digits. If the lines have trailing spaces, carriage returns, or any extra characters, the pattern fails. This is the most likely cause of no output when the visible content appears to match.

Why this answer

The regex anchors to the full line with ^ and $. If the lines contain trailing whitespace, carriage returns, or other invisible characters, the pattern will not match despite the visible text appearing correct. This is the most common cause of such a failure when the regex itself is syntactically valid.

Exam trap

The trap here is assuming the regex is wrong when the actual issue is invisible characters like trailing spaces or CRLF line endings that break the $ anchor.

51
MCQmedium

What does the command 'history' do?

A.It shows the date and time of file creation.
B.It displays the system log history.
C.It deletes all previous command history.
D.It prints the command history list.
AnswerD

The `history` builtin prints the numbered list of previously executed commands held in the shell's in-memory history list, satisfying the stem's request directly. It neither re-runs commands nor edits the list; those require separate mechanisms such as `!!` expansion or `history -c`.

Why this answer

The 'history' command in Bash and other POSIX shells prints the numbered list of previously executed commands stored in the shell's history buffer (typically ~/.bash_history). It is a shell builtin, not an external binary, and it reads from the in-memory history list maintained per session. This is the standard way to review and recall prior commands for reuse or auditing.

Exam trap

The trap here is confusing 'history' (shell command history) with system logging tools like journalctl or /var/log files — candidates who have not used the shell interactively may pick the 'system log' option.

How to eliminate wrong answers

Option A is wrong because file creation timestamps are shown by 'ls -l', 'stat', or 'ls --time=creation' — 'history' has nothing to do with filesystem metadata. Option B is wrong because system logs are viewed with 'journalctl', 'dmesg', or files under /var/log — 'history' only shows the current user's shell command history, not system events. Option C is wrong because 'history -c' clears the history list; plain 'history' only displays it, and it never deletes anything.

52
MCQeasy

How do you check the exit status of the last command executed?

A.echo $#
B.echo $!
C.echo $?
D.echo $status
AnswerC

The shell stores the previous command's exit status in the special parameter $?, so echo $? prints that numeric value. This directly satisfies the requirement to check whether the last executed command succeeded or failed.

Why this answer

In POSIX shells (bash, sh, dash), the special parameter $? expands to the exit status of the most recently executed foreground pipeline. A value of 0 conventionally indicates success, while any non-zero value (1-255) signals failure or a specific error condition. This is the standard mechanism scripts use to branch on command success, e.g., 'if [ $? -eq 0 ]; then ...'.

Exam trap

The trap here is confusing the shell's many dollar-sign special parameters — candidates who memorise '$!' as 'last something' often pick it for exit status instead of process ID.

How to eliminate wrong answers

Option A is wrong because $# expands to the number of positional parameters passed to the current shell or function, not the exit status. Option B is wrong because $! expands to the PID of the most recently backgrounded process, useful for job control but unrelated to exit codes. Option D is wrong because $status is a csh/tcsh variable, not a POSIX shell parameter; in bash it would simply expand to an empty string unless a variable of that name was explicitly set.

53
MCQeasy

A new administrator wants to see the manual page for the `cp` command to check its options. Which command should they run?

A.info cp
B.man cp
C.doc cp
D.help cp
AnswerB

The man command displays the manual page for a given command. Running `man cp` opens the cp manual, where the administrator can read about options like -r, -p, and -i. This is the standard way to access documentation on a Linux system for commands and configuration files.

Why this answer

The man command is the standard interface to the system's manual pages. Running `man cp` displays the cp manual, covering syntax, options, and examples. Other tools like help are limited to shell built-ins, and info or custom doc commands are not universally available or standard for this purpose.

Exam trap

The trap here is assuming help works for all commands, when it only covers shell built-ins and not external binaries like cp.

54
MCQmedium

To sort the lines of a text file numerically instead of alphabetically, which flag do you use?

A.sort -n
B.sort -f
C.sort -u
D.sort -r
AnswerA

The `-n` flag makes `sort` compare lines by their leading numeric value rather than lexicographically, so "10" ranks after "9" instead of before it. This directly satisfies the stem's requirement to sort numerically rather than alphabetically, which is the default byte-order comparison.

Why this answer

The -n flag tells sort to perform a numeric sort, comparing lines by their numeric value rather than lexicographically. This is essential when sorting numbers like 2, 10, 100, which would otherwise sort as 10, 100, 2 alphabetically.

Exam trap

The trap is confusing -n (numeric) with -r (reverse) or -u (unique) — candidates who remember 'sort has a flag for numbers' may pick -r thinking 'r' stands for 'rank' or numeric ordering.

How to eliminate wrong answers

Option B is wrong because sort -f ignores case (folds lowercase to uppercase) during sorting — it is a case-insensitivity flag, not a numeric flag. Option C is wrong because sort -u removes duplicate lines (unique), which is unrelated to numeric sorting. Option D is wrong because sort -r reverses the sort order (descending instead of ascending) but still sorts lexicographically unless combined with -n.

55
Multi-Selectmedium

Which THREE of the following represent ways to manipulate command output in a pipe?

Select 3 answers
A.grep
B.cd
C.pwd
D.wc
E.sed
AnswersA, D, E

grep filters piped input line by line, printing only lines matching a specified pattern. This is a standard way to manipulate command output within a pipeline, satisfying the stem's requirement, unlike commands that merely display or redirect output.

Why this answer

grep is correct because it filters piped input line-by-line using pattern matching and emits only matching lines to stdout, which is a classic way to manipulate command output in a pipe. wc is correct because it consumes piped input and produces counts of lines, words, and bytes (with -l, -w, -c), transforming the stream into summary output. sed is correct because it is a stream editor that reads piped input and performs non-interactive text transformations such as substitution (s/old/new/g), deletion, and printing, directly manipulating the output stream. cd is not correct because it is a shell builtin that changes the current working directory and does not read stdin or transform piped data. pwd is not correct because it simply prints the current working directory name and does not accept or manipulate piped input.

Exam trap

The trap is confusing shell builtins that produce output (pwd) with true filters; candidates may assume any command that prints text can be piped, but only stdin-consuming filters work in a pipeline.

56
MCQhard

You want to replace every occurrence of 'old' with 'new' in the file 'data.txt' and view the result in the terminal. Which command achieves this?

A.sed 's/old/new/g' data.txt
B.sed 's/old/new/' data.txt
C.grep 's/old/new/g' data.txt
D.sed 'r/old/new/g' data.txt
AnswerA

The sed substitute command with the g flag replaces every match on each line, and without -i it writes to standard output, so results appear in the terminal while data.txt stays untouched. This satisfies both the replace-all and view-in-terminal constraints.

Why this answer

The sed command with the 's/old/new/g' expression performs a global substitution, replacing every occurrence of 'old' with 'new' on each line of data.txt. By default, sed writes the modified content to standard output, which displays the result in the terminal without altering the original file. The 'g' flag is essential because without it, only the first occurrence per line would be replaced.

Exam trap

The trap is forgetting the 'g' flag — candidates often assume sed replaces all occurrences by default, but it only replaces the first match per line unless 'g' is specified.

How to eliminate wrong answers

Option B is wrong because omitting the 'g' flag means sed replaces only the first occurrence of 'old' on each line, not every occurrence as the question requires. Option C is wrong because grep is a search tool, not a substitution tool — it does not interpret 's/old/new/g' as a sed expression and would instead search for that literal pattern. Option D is wrong because 'r' is not a valid sed substitution command; the correct command is 's' for substitute, and using 'r' would cause sed to error or behave unexpectedly.

57
MCQmedium

A user wants to find all files in the /var/log directory that have been modified in the last 24 hours and display their names. Which command accomplishes this?

A.ls -l /var/log | grep '24'
B.find /var/log -name '*.log' -newer /tmp/timestamp
C.find /var/log -type f -mtime -1
D.grep -r '24' /var/log
AnswerC

The find command with -type f limits the search to regular files, and -mtime -1 selects files modified less than 24 hours ago. This directly matches the requirement to find files modified in the last day and list their names.

Why this answer

The find command is the standard tool for searching files based on metadata such as modification time. Using -mtime -1 correctly selects files modified within the last 24 hours, and -type f restricts to regular files. Other commands like ls with grep, find with -newer, or grep -r do not accurately or completely fulfill the task.

Exam trap

The trap here is thinking that listing directory contents with ls and filtering with grep can reliably find files modified within a specific time frame.

58
Multi-Selectmedium

Which THREE of the following are environment variables commonly seen in a Linux shell?

Select 3 answers
A.USER
B.ROOT
C.HOME
D.FILE
E.PATH
AnswersA, C, E

USER holds the current logged-in account's username and is exported by the login process, making it a standard shell environment variable. It is inherited by child processes, matching the stem's criterion of variables commonly present in a Linux shell.

Why this answer

USER is a standard environment variable that holds the username of the currently logged-in user, so option A is correct. HOME is a standard environment variable that specifies the current user's home directory path, making option C correct. PATH is a standard environment variable that lists the directories the shell searches for executable commands, so option E is correct.

ROOT (B) is not a standard environment variable; it is a common username or directory name, not a shell variable. FILE (D) is not a standard environment variable either; it is only a special variable inside Makefiles, not a general Linux shell environment variable.

Exam trap

The trap is picking plausible-sounding names like ROOT or FILE that resemble real variables but are not part of the standard Linux environment — candidates must recall the actual canonical set.

59
MCQhard

You want to search for a file named 'notes.txt' starting from the root directory. Which command is most efficient?

A.grep -r notes.txt /
B.find / -name notes.txt
C.search notes.txt /
D.ls -R / | grep notes.txt
AnswerB

find / -name notes.txt begins traversal at the root directory and recurses through every subdirectory, matching the filename exactly. This guarantees the file is located wherever it resides, satisfying the requirement to search from root.

Why this answer

The `find` command is purpose-built for filesystem traversal and can search by name, size, time, permissions, and more. Running `find / -name notes.txt` starts at the root directory and recursively descends every subdirectory, matching files whose basename is exactly 'notes.txt'. It is the most efficient and correct tool because it operates directly on directory entries via the kernel, without spawning extra processes per file.

Exam trap

The trap is confusing filename search with content search — candidates see 'search for a file' and pick grep because grep is the famous search tool, forgetting that grep searches inside files while find searches for the files themselves.

How to eliminate wrong answers

Option A is wrong because `grep -r notes.txt /` searches file *contents* for the string 'notes.txt', not filenames — it would return files containing that text, and it would also try to read binary files and /proc entries, producing errors. Option C is wrong because `search` is not a Linux command at all; it is a Windows/DOS concept. Option D is wrong because `ls -R /` recursively lists directory contents and pipes to grep, which is far less efficient (spawns ls, buffers entire output, then filters) and can miss files due to output truncation or permission errors.

Ready to test yourself?

Try a timed practice session using only The Power OF The Command Line questions.