Linux Commands Questions with Answers

9 practice questions on Linux Commands (Git & DevOps) with answers and step-by-step solutions. Try 9 samples below, then practise all 9 free in the AptiNest app.

Sample questions

Question 1Easy

What does kill -9 <pid> do?

  1. Asks the process to exit gracefully by sending SIGTERM
  2. Restarts the process with the same arguments and PID
  3. Sends SIGKILL, which cannot be caught and ends it at once
  4. Pauses the process until it receives a SIGCONT signal

Answer: C. Sends SIGKILL, which cannot be caught and ends it at once

Explanation

Signal 9 is SIGKILL. Plain kill sends SIGTERM (15), which the process can handle to clean up first.

Step-by-step solution

  1. -9 is signal number 9, SIGKILL.
  2. SIGKILL cannot be caught or ignored.
  3. The kernel ends the process at once, with no cleanup.

Why the other options are wrong

  • A. SIGTERM (15) is what plain kill sends.
  • B. kill never restarts processes.
  • D. Pausing is SIGSTOP.

Tip: kill = polite (15); kill -9 = forced.

Question 2Easy

What is the difference between > and >> in a shell?

  1. > overwrites the file, >> appends to it
  2. > appends, >> overwrites
  3. Both append
  4. > redirects input, >> redirects output

Answer: A. > overwrites the file, >> appends to it

Explanation

> truncates the target file before writing, while >> adds to the end.

Step-by-step solution

  1. > truncates the file, then writes.
  2. >> adds to the end of the file.
  3. Both create the file if needed.

Why the other options are wrong

  • B. That reverses them.
  • C. > overwrites.
  • D. < redirects input; both > and >> redirect output.

Tip: One > overwrites; two >> append.

Question 3Easy

Which grep option makes the search case-insensitive?

  1. -i
  2. -v
  3. -c
  4. -r

Answer: A. -i

Explanation

-i ignores case. -v inverts the match, -c counts matching lines and -r searches recursively.

Step-by-step solution

  1. -i means ignore case.
  2. grep -i error matches error, Error and ERROR.

Why the other options are wrong

  • B. -v inverts the match.
  • C. -c counts matching lines.
  • D. -r searches recursively.

Tip: -i ignore case, -v invert, -c count, -r recursive.

Question 4Easy

What is printed?

printf 'b\na\nc\n' | sort | head -n 1
  1. c
  2. a b c
  3. b
  4. a

Answer: D. a

Explanation

printf prints three lines, sort orders them alphabetically, and head -n 1 keeps only the first line.

Step-by-step solution

  1. printf outputs three lines: b, a, c.
  2. sort orders them: a, b, c.
  3. head -n 1 keeps only the first: a.

Why the other options are wrong

  • A. c is last after sorting.
  • B. head -n 1 keeps one line, not all three.
  • C. b is first before sorting, but sort runs before head.

Tip: sort | head -n 1 = smallest line.

Question 5Easy

Which command prints the current working directory?

  1. whoami
  2. ls
  3. cd
  4. pwd

Answer: D. pwd

Explanation

pwd stands for print working directory.

Step-by-step solution

  1. pwd = print working directory.
  2. It shows the full path of the current directory.

Why the other options are wrong

  • A. whoami prints the user name.
  • B. ls lists files.
  • C. cd changes directory.

Tip: Lost in the shell? pwd.

Question 6Easy

What is printed?

echo "root:x:0:0" | cut -d: -f3
  1. root
  2. x
  3. 0
  4. :

Answer: C. 0

Explanation

cut -d: splits on colons and -f3 selects the third field. Here both the third and fourth fields are 0.

Step-by-step solution

  1. -d: splits on colons: root | x | 0 | 0.
  2. -f3 picks the third field.
  3. That is 0.

Why the other options are wrong

  • A. root is field 1.
  • B. x is field 2.
  • D. The delimiter itself is not printed.

Tip: cut -d<sep> -f<n> = nth field.

Question 7Medium

What is printed?

false
echo $?
  1. Nothing
  2. 1
  3. false
  4. 0

Answer: B. 1

Explanation

$? holds the exit status of the last command. false always exits with 1, and 0 would mean success.

Step-by-step solution

  1. false does nothing and exits with status 1.
  2. $? holds the last command's exit status.
  3. So echo prints 1.

Why the other options are wrong

  • A. echo always prints something, here the status.
  • C. $? is a number, not the command name.
  • D. 0 means success; false always fails.

Tip: 0 = success; non-zero = failure.

Question 8Medium

After chmod 754 script.sh, what are the permissions?

  1. rwxr-xr--
  2. rwxrwxr--
  3. rw-r-xr--
  4. rwxr--r-x

Answer: A. rwxr-xr--

Explanation

Each digit is a sum of read (4), write (2) and execute (1). 7 = rwx for the owner, 5 = r-x for the group, 4 = r-- for others.

Step-by-step solution

  1. Each digit is read 4 + write 2 + execute 1.
  2. 7 = 4+2+1 = rwx (owner).
  3. 5 = 4+1 = r-x (group); 4 = r-- (others).
  4. So rwxr-xr--.

Why the other options are wrong

  • B. rwxrwxr-- is 774.
  • C. rw-r-xr-- is 654.
  • D. rwxr--r-x is 745.

Tip: r=4, w=2, x=1; add them per digit.

Question 9Hard

missing does not exist. Which command saves both the normal output and the error message into out.txt?

  1. ls missing 2>&1 > out.txt
  2. ls missing > out.txt 2>&1
  3. ls missing > out.txt
  4. ls missing 2> out.txt

Answer: B. ls missing > out.txt 2>&1

Explanation

Redirections are processed left to right. > out.txt first points stdout at the file, then 2>&1 points stderr wherever stdout now goes. In the other order, stderr is copied to the terminal before stdout is redirected.

Step-by-step solution

  1. Redirections run left to right.
  2. > out.txt points stdout at the file.
  3. 2>&1 then points stderr wherever stdout now goes: the file.
  4. So both outputs land in out.txt.

Why the other options are wrong

  • A. Here 2>&1 copies stderr to the terminal first, then only stdout goes to the file.
  • C. Only stdout is saved; the error still goes to the terminal.
  • D. Only stderr is saved.

Tip: File first, then 2>&1.

Practise all 9 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Git & DevOps topics · All topics