Hack4Hack

Linux practice questions with answers

Practise Linux questions at your own pace: shell commands, permissions, pipes, processes, signals and troubleshooting. Pick an answer to see whether you are right and why. Nothing is timed or ranked here; for that, play the Daily Quiz at 4 PM IST.

  1. Which command lists files including hidden ones, with details?

    easy

    1. ls
    2. ls -la
    3. dir
    4. cat -a

    Answer: B. ls -la

    Explanation: -l gives the long format (permissions, owner, size, date) and -a includes dotfiles.

  2. Which command shows the current directory?

    easy

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

    Answer: B. pwd

    Explanation: pwd prints the working directory. whoami prints the current user.

  3. Which command searches for text inside files?

    easy

    1. find
    2. grep
    3. locate
    4. which

    Answer: B. grep

    Explanation: grep -rn "text" . searches recursively with line numbers. find searches for files by name, size or date.

  4. What does chmod 755 script.sh set?

    easy

    1. Everyone can read and write
    2. The owner can read, write and execute; group and others can read and execute
    3. Only the owner can read
    4. Nobody can execute

    Answer: B. The owner can read, write and execute; group and others can read and execute

    Explanation: 7 = rwx (4 + 2 + 1), 5 = r-x (4 + 1), for owner, group and others.

  5. What does this pipeline print?

    medium

    printf "b\na\nb\nc\n" | sort | uniq -c | sort -rn | head -1
    1. 2 b
    2. 1 a
    3. b
    4. a b c

    Answer: A. 2 b

    Explanation: sort groups duplicates, uniq -c counts them, sort -rn puts the biggest count first, head -1 keeps the top line: b appears twice.

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

    medium

    1. None
    2. > overwrites the file; >> appends to it
    3. >> overwrites; > appends
    4. > is for errors only

    Answer: B. > overwrites the file; >> appends to it

    Explanation: 2> redirects standard error, and 2>&1 sends errors to the same place as standard output.

  7. How do you find which process is listening on port 8080?

    medium

    1. ps 8080
    2. lsof -i :8080 (or ss -ltnp)
    3. kill 8080
    4. top -p 8080

    Answer: B. lsof -i :8080 (or ss -ltnp)

    Explanation: lsof lists open files and sockets; ss is the modern replacement for netstat.

  8. What does kill -9 do differently from kill?

    medium

    1. Nothing
    2. kill sends SIGTERM, which a process can handle to shut down cleanly; kill -9 sends SIGKILL, which cannot be caught
    3. kill -9 restarts the process
    4. kill -9 only works for root

    Answer: B. kill sends SIGTERM, which a process can handle to shut down cleanly; kill -9 sends SIGKILL, which cannot be caught

    Explanation: Try SIGTERM first so the program can flush files and close connections. Use SIGKILL only when it hangs.

  9. What does this print?

    hard

    echo "a b c" | awk '{print $2}'
    1. a
    2. b
    3. c
    4. a b c

    Answer: B. b

    Explanation: awk splits each line into fields on whitespace; $2 is the second field. $0 is the whole line and $NF the last field.

  10. A disk is full but du shows little usage. What is a common cause?

    hard

    1. A broken du command
    2. A deleted file that a running process still holds open, so its space is not freed
    3. Too many directories
    4. The file system is read-only

    Answer: B. A deleted file that a running process still holds open, so its space is not freed

    Explanation: lsof +L1 lists deleted-but-open files. Restarting or signalling the process (e.g. to reopen its logs) frees the space.

  11. What does the "load average" in top or uptime mean?

    hard

    1. CPU temperature
    2. The average number of processes running or waiting to run (and, on Linux, in uninterruptible I/O) over 1, 5 and 15 minutes
    3. Memory used in GB
    4. Network traffic

    Answer: B. The average number of processes running or waiting to run (and, on Linux, in uninterruptible I/O) over 1, 5 and 15 minutes

    Explanation: Compare it with the number of CPU cores: a load of 8 on a 4-core machine means work is queuing.

  12. Which are true about file permissions and ownership? (Select all that apply)

    hard

    1. chown changes a file's owner
    2. The execute bit on a directory lets you enter it (cd)
    3. sudo runs a command with another user's (usually root's) privileges
    4. chmod 777 is a safe default for web servers

    Answer: A. chown changes a file's owner; B. The execute bit on a directory lets you enter it (cd); C. sudo runs a command with another user's (usually root's) privileges

    Explanation: 777 lets any user change the files: a common security hole. Give the least permission that works.

Play the Daily Quiz at 4 PM IST