pwd (print working directory) in Linux CLI - Time & Space Complexity
Let's see how the time it takes to run the pwd command changes as we use it in different situations.
We want to know how the command's work grows when the environment changes.
Analyze the time complexity of the following command.
pwd
This command prints the current directory path you are in.
Look for any repeated work inside the command.
- Primary operation: The command reads the current directory path from the system.
- How many times: It does this once each time you run
pwd.
The time to run pwd grows linearly with the length of the directory path.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 (short path) | Proportional to 10 |
| 100 (longer path) | Proportional to 100 |
| 1000 (very long path) | Proportional to 1000 |
Pattern observation: The work increases linearly with longer paths.
Time Complexity: O(n)
This means the time to run pwd increases linearly with the directory depth.
[X] Wrong: "The longer the directory path, the longer pwd takes to run."
[OK] Why it's right: The system typically builds the full path by walking up each directory, so pwd takes longer with deeper directories.
Understanding simple commands like pwd helps build a strong foundation for more complex scripting tasks.
"What if pwd had to build the full path by walking up each directory? How would the time complexity change?"
Practice
pwd command do in a Linux terminal?Solution
Step 1: Understand the purpose of
Thepwdpwdcommand stands for 'print working directory'. It shows the full path of the directory you are currently in.Step 2: Compare with other options
Options A, C, and D describe other commands likels,cd, andrm, which do not print the current directory path.Final Answer:
Shows the full path of the current directory you are in -> Option DQuick Check:
pwd= current directory path [OK]
- Confusing pwd with ls (list files)
- Thinking pwd changes directory
- Assuming pwd deletes files
Solution
Step 1: Identify the standard command
The standard command to print the current directory is exactlypwdwithout any extra letters or options.Step 2: Check other options for validity
Options B, C, and D are not valid Linux commands for this purpose.pwd -lis not a standard option, andprintwdandshowdirdo not exist.Final Answer:
pwd -> Option AQuick Check:
Correct syntax = pwd [OK]
- Adding wrong options like -l
- Using non-existent commands
- Confusing with print working directory spelled out
cd /usr/local pwdWhat will be the output of
pwd?Solution
Step 1: Understand the
The commandcdcommand effectcd /usr/localchanges the current directory to/usr/local.Step 2: Check what
After changing directory,pwdoutputspwdprints the full path of the current directory, which is/usr/local.Final Answer:
/usr/local -> Option BQuick Check:
cd changes directory, pwd shows it [OK]
- Assuming pwd shows only part of the path
- Confusing directory names
- Thinking pwd shows home directory always
pwd but get an error: command not found. What is the most likely cause?Solution
Step 1: Analyze the error message
The errorcommand not foundmeans the shell cannot find the command you typed.Step 2: Identify common causes
This usually happens if the command is misspelled or typed incorrectly. Being root or directory contents do not affectpwdavailability.Final Answer:
You typedpwdincorrectly or misspelled it -> Option CQuick Check:
Misspelling commands causes 'command not found' [OK]
- Assuming permissions cause 'command not found'
- Thinking empty directory breaks pwd
- Restarting terminal unnecessarily
current_dir.txt (overwriting it if it exists). Which command correctly does this?Solution
Step 1: Understand output redirection
The symbol>redirects the output of a command to a file, overwriting it.Step 2: Apply redirection to
Usingpwdpwd > current_dir.txtwrites the current directory path into the filecurrent_dir.txt.Step 3: Check other options
pwd >> current_dir.txtappends instead of overwriting. Options A and C are invalid syntax.Final Answer:
pwd > current_dir.txt -> Option AQuick Check:
Use > to save command output to a file [OK]
- Using pipe (|) incorrectly to file
- Forgetting > or >> for redirection
- Using command with filename as argument
Ready to prove it?
Pass the test and this pattern moves unknown
