Skip to content
Clairos Learn

Shell scripting · Lesson 5 of 31 · 8 minutes

Dirname in scripts

Use dirname to reason about script paths.

Mastery 0/3Run itExplain itApply it
learner@clairosSimulated Linux lab
learner@clairos:~ $
↑ history · Tab complete · Ctrl+C cancel · Esc, Tab to leaveExit 0

Understand the pattern

Junior operators build confidence by gathering evidence before changing systems.

dirname
Extract the directory portion of a path string.

A path can be syntactically valid without naming an existing directory.

pwd
Print the directory that relative paths currently resolve from.

A relative path is interpreted from the working directory, not necessarily the home folder.

Worked example and expected result
$ dirname scripts/deploy.sh
/scripts

Exit status: 0. The command completed successfully in the training fixture.

$ dirname /etc/nginx/sites-available/learn.conf
/etc/nginx/sites-available

Exit status: 0. The command completed successfully in the training fixture.

$ pwd
/home/learner

Exit status: 0. The command completed successfully in the training fixture.

Try a variation

Repeat this workflow in the sandbox with a different scratch filename. Keep the system fixtures unchanged and compare the observed output with your expectation before moving on.

Explain it

In “Dirname in scripts”, which interpretation is safe?

Apply it elsewhere

Before adapting the dirname workflow from “Dirname in scripts” to another server, what should you do?

Keep the momentum.

Complete the lab, then explain and apply the pattern.

Next: Sed config previewTry an incident drill

Anonymous lesson and mastery progress lasts for this session. Sign in to keep synced lesson XP between sessions; mastery stays device-local. No speed bonus, lost lives, or hint penalties.

Keep learning

  1. 01Basename in scriptsUse basename to report script inputs.Practice
  2. 02Sed config previewPreview config text replacement as a script step.Practice
  3. 03Awk reportsBuild a small report from fields.Practice