LPIC-1 Shells, Scripting and Data Management Practice Question
Which command returns the directory part of a full path like '/home/user/script.sh'?
⚠ Common exam trap
The trap is that candidates may think `dirname` is only one of multiple correct answers, but the question explicitly asks for 'which command', making `dirname` the single correct command. Parameter expansions are not commands.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
dirname /home/user/script.sh
The `dirname` command extracts the directory portion of a full path, returning `/home/user` for the given example. Option B is correct because it directly answers the question—`dirname` is a standard command for this purpose. Option A uses incorrect syntax, Option C describes a parameter expansion (not a command), and Option D (`basename`) returns the filename, not the directory.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
echo ${/home/user/script.sh%/*}
Why it's wrong here
Incorrect. The syntax `${/home/user/script.sh%/*}` is invalid; parameter expansion requires a variable name, not a literal string.
- ✓
dirname /home/user/script.sh
Why this is correct
Correct. `dirname` is the standard command to return the directory part of a path.
- ✗
Either dirname or the expansion ${path%/*} if path is set to the full path
Why it's wrong here
Incorrect. While both `dirname` and `${path%/*}` are valid methods, the question asks for a command; `dirname` is the command, but option C presents it as 'either', which does not answer the question directly.
- ✗
basename /home/user/script.sh
Why it's wrong here
Incorrect. `basename` returns the filename component (e.g., `script.sh`), not the directory.
Go deeper
Related to this question
About these practice questions
This LPIC-1 question is part of Courseiva's 527-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This LPIC-1 practice question is part of Courseiva's free LPI certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the LPIC-1 exam.