File attributes and operations in Operating Systems - Time & Space Complexity
When working with file attributes and operations, it is important to understand how the time taken changes as the number of files or operations grows.
We want to know how the cost of checking or modifying file attributes scales with more files or repeated actions.
Analyze the time complexity of the following code snippet.
for each file in directory:
read file attributes
if file is writable:
update last modified time
end if
end for
This code goes through every file in a directory, reads its attributes, and updates the last modified time if the file can be written to.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each file in the directory.
- How many times: Once for every file present in the directory.
As the number of files increases, the total time grows proportionally because each file requires reading attributes and possibly updating.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 attribute reads and checks |
| 100 | About 100 attribute reads and checks |
| 1000 | About 1000 attribute reads and checks |
Pattern observation: The work grows steadily and directly with the number of files.
Time Complexity: O(n)
This means the time needed increases in direct proportion to the number of files being processed.
[X] Wrong: "Updating file attributes takes the same time no matter how many files there are."
[OK] Correct: Each file requires separate operations, so more files mean more total time.
Understanding how file operations scale helps you reason about system performance and resource use in real tasks.
"What if the code also recursively processes files in subdirectories? How would the time complexity change?"
Practice
Solution
Step 1: Understand common file attributes
Common file attributes include size, creation date, and permissions which describe the file's properties.Step 2: Identify the incorrect attribute
File color is not a standard attribute tracked by operating systems; it is usually a display feature in some file managers.Final Answer:
File color -> Option AQuick Check:
File color is not a file attribute [OK]
- Confusing display features with file attributes
- Thinking file color is stored as metadata
Solution
Step 1: Recall commands related to file permissions
chmod is the command used to change file permissions, allowing read, write, and execute rights to be set.Step 2: Differentiate from other commands
chown changes file ownership, ls lists files, and rm deletes files, so they do not change permissions.Final Answer:
chmod -> Option CQuick Check:
chmod changes permissions [OK]
- Confusing chmod with chown
- Using ls or rm for permissions
import os file_path = 'example.txt' size = os.path.getsize(file_path) print(size)What does this code output?
Solution
Step 1: Understand os.path.getsize function
This function returns the size of the file in bytes, not lines or dates.Step 2: Analyze the print output
Printing the size variable outputs the file size in bytes as an integer.Final Answer:
The size of the file in bytes -> Option BQuick Check:
os.path.getsize returns file size [OK]
- Thinking it counts lines
- Confusing size with creation date
mv file1.txt file2.txt/
Solution
Step 1: Understand mv command syntax
mv renames or moves files. The destination should be a file name or directory.Step 2: Analyze the trailing slash effect
A trailing slash after file2.txt makes the system treat it as a directory, which likely does not exist, causing an error.Final Answer:
The trailing slash after file2.txt treats it as a directory -> Option AQuick Check:
Trailing slash means directory [OK]
- Thinking mv deletes files
- Assuming mv copies files
- Believing -r is needed for mv
Solution
Step 1: Understand the requirement
The task is to list files filtered by size, so size attribute must be checked.Step 2: Evaluate each option
List all files, then check each file's size attribute and print only those >1MB correctly filters files by size before printing. List all files and print their names without checking size ignores size. Delete files smaller than 1MB and then list remaining files deletes files, which is unsafe. Change permissions of files larger than 1MB before listing changes permissions unnecessarily.Final Answer:
List all files, then check each file's size attribute and print only those >1MB -> Option DQuick Check:
Filter by size attribute before listing [OK]
- Ignoring file size attribute
- Deleting files instead of filtering
- Changing permissions unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
