First Linux commands in Linux CLI - Time & Space Complexity
When we run Linux commands, some take longer depending on what they do. Understanding how their work grows with input helps us know what to expect.
We want to see how the time to run these commands changes as the input or data size grows.
Analyze the time complexity of the following Linux command sequence.
# List all files in a directory
ls /path/to/directory
# Count the number of lines in a file
wc -l filename.txt
# Search for a word in a file
grep "word" filename.txt
These commands list files, count lines, and search text in files.
Look at what repeats inside these commands:
- Primary operation: Reading each file or directory entry one by one.
- How many times: Once for each file or line in the input.
As the number of files or lines grows, the commands take longer roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 files or lines | About 10 reads |
| 100 files or lines | About 100 reads |
| 1000 files or lines | About 1000 reads |
Pattern observation: The work grows linearly with the number of files or lines.
Time Complexity: O(n)
This means the time grows in a straight line as the input size grows.
[X] Wrong: "These commands always run instantly no matter how big the files are."
[OK] Correct: Actually, the commands read through all files or lines, so bigger inputs take more time.
Knowing how command time grows helps you understand system behavior and write better scripts. This skill shows you think about efficiency in real tasks.
"What if we used 'grep' on multiple files at once? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of
Thepwdpwdcommand stands for "print working directory" and shows the current folder path.Step 2: Compare with other commands
lslists files,cdchanges directory, andmkdircreates a folder, so they don't show the current directory.Final Answer:
pwd-> Option AQuick Check:
Current directory command = pwd [OK]
- Confusing ls with pwd
- Using cd to show directory
- Thinking mkdir shows current folder
projects?Solution
Step 1: Identify the command to create directories
The standard Linux command to make a directory ismkdir.Step 2: Check the syntax
The correct syntax ismkdir directory_name, somkdir projectsis correct.Final Answer:
mkdir projects-> Option AQuick Check:
Create directory command = mkdir [OK]
- Using mk instead of mkdir
- Typing create instead of mkdir
- Using newdir which is not a command
mkdir testdir cd testdir pwd
Solution
Step 1: Understand the commands
mkdir testdircreates a folder named testdir.cd testdirmoves into that folder.pwdprints the full path of the current directory.Step 2: Determine the full path
Assuming the user starts in their home directory (like /home/username), after changing into testdir,pwdwill show /home/username/testdir.Final Answer:
/home/username/testdir -> Option CQuick Check:
pwd after cd into testdir = full path /home/username/testdir [OK]
- Expecting just folder name output
- Assuming root directory path
- Confusing relative and absolute paths
cd myfolder but get an error: bash: cd: myfolder: No such file or directory. What is the most likely cause?Solution
Step 1: Analyze the error message
The error says "No such file or directory", meaning the folder namedmyfolderis not found in the current location.Step 2: Check other options
cdis a basic command and usually not misspelled here. Permissions errors give different messages.mkdircreates folders but is not required beforecdif the folder exists.Final Answer:
The foldermyfolderdoes not exist in the current directory -> Option BQuick Check:
cd error means folder missing [OK]
- Assuming permission error
- Thinking cd needs mkdir first
- Misspelling cd command
backup inside your current directory and then move into it. Which sequence of commands will do this correctly?Solution
Step 1: Create the folder first
The commandmkdir backupcreates the folder named backup in the current directory.Step 2: Change directory into the new folder
After creating it,cd backupmoves you inside that folder.Step 3: Check other options
cd backup mkdir backuptries to cd into backup before it exists, causing an error.mkdir cd backupmisses the folder name in mkdir.cd backup cd backuptries to cd twice without creating the folder.Final Answer:
mkdir backup cd backup-> Option DQuick Check:
Create then cd = mkdir then cd [OK]
- Trying cd before mkdir
- Omitting folder name in mkdir
- Repeating cd without folder
Ready to prove it?
Pass the test and this pattern moves unknown
