Temporary files (mktemp) in Bash Scripting - Time & Space Complexity
When creating temporary files in a script, it is important to understand how the time to create these files grows as you create more of them.
We want to know how the script's running time changes when making many temporary files using mktemp.
Analyze the time complexity of the following code snippet.
for i in $(seq 1 $n); do
tmpfile=$(mktemp)
echo "Temporary file created: $tmpfile"
done
This script creates n temporary files one after another and prints their names.
- Primary operation: The
forloop runsmktempcommandntimes. - How many times: Exactly once per loop iteration, so
ntimes total.
Each time we increase n, the number of temporary files created grows linearly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to mktemp |
| 100 | 100 calls to mktemp |
| 1000 | 1000 calls to mktemp |
Pattern observation: The total work grows directly in proportion to n. Double n, double the work.
Time Complexity: O(n)
This means the time to create temporary files grows linearly with the number of files you want to create.
[X] Wrong: "Creating temporary files with mktemp is instant and does not depend on how many files I create."
[OK] Correct: Each call to mktemp takes some time, so creating many files adds up and takes longer as n grows.
Understanding how loops and external commands like mktemp scale helps you write efficient scripts and explain your reasoning clearly in interviews.
What if we created temporary files inside a nested loop? How would the time complexity change?
Practice
mktemp command in bash scripting?Solution
Step 1: Understand the function of
mktempmktempis designed to create temporary files or directories with unique names to avoid conflicts.Step 2: Compare with other options
Options A, C, and D describe unrelated commands or actions not related tomktemp.Final Answer:
To create a unique temporary file or directory safely -> Option DQuick Check:
mktemp= unique temp file creation [OK]
- Confusing mktemp with file deletion commands
- Thinking mktemp lists files
- Assuming mktemp changes file permissions
mktemp in bash?Solution
Step 1: Identify basic
Runningmktempusagemktempalone creates a temporary file with a unique name.Step 2: Understand options
-dcreates a temporary directory, not a file. Options--removeand-rare invalid or not standard.Final Answer:
mktemp -> Option AQuick Check:
Basicmktemp= create temp file [OK]
- Using -d when a file is needed, creates directory instead
- Using non-existent options like --remove or -r
- Confusing directory creation with file creation
tempfile=$(mktemp) echo $tempfile rm $tempfile
Solution
Step 1: Analyze
mktempusagemktempcreates a unique temporary file and returns its path, stored intempfile.Step 2: Understand echo and rm commands
echo $tempfileprints the temporary filename.rm $tempfiledeletes the file to keep the system clean.Final Answer:
Prints a unique temporary filename, then deletes it -> Option BQuick Check:
mktemp creates file, echo prints, rm deletes [OK]
- Expecting literal 'tempfile' output instead of variable value
- Thinking tempfile is empty or unset
- Confusing file creation with directory creation
tempfile=mktemp echo $tempfile rm $tempfile
Solution
Step 1: Check variable assignment
Assigningtempfile=mktempsets the string 'mktemp', not the command output.Step 2: Correct command substitution
Usetempfile=$(mktemp)to run the command and assign its output.Final Answer:
Missing parentheses to execute mktemp command -> Option AQuick Check:
Use $(mktemp) to assign command output [OK]
- Assigning command name as string instead of output
- Forgetting $() or backticks for command substitution
- Thinking rm order affects error here
tempdir?Solution
Step 1: Understand
The-doption in mktemp-doption tellsmktempto create a temporary directory instead of a file.Step 2: Correct command substitution syntax
Use$(mktemp -d)to execute the command and assign the directory path totempdir.Final Answer:
tempdir=$(mktemp -d) -> Option CQuick Check:
Use $(mktemp -d) for temp directory creation [OK]
- Assigning command as string without $()
- Using mktemp without -d creates a file, not directory
- Using invalid options like -f
Ready to prove it?
Pass the test and this pattern moves unknown
