0
0
Bash Scriptingscripting~5 mins

Temporary files (mktemp) in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of the mktemp command in bash scripting?
The mktemp command creates a unique temporary file or directory safely, avoiding name conflicts and security issues.
Click to reveal answer
beginner
How does mktemp ensure the temporary file name is unique?
mktemp generates a random string in the file name template, replacing placeholders like XXXXXX to create a unique name.
Click to reveal answer
beginner
Write a simple mktemp command to create a temporary file with prefix tempfile_.
Use: mktemp tempfile_XXXXXX. This creates a file like /tmp/tempfile_a1b2c3 in the system's temporary directory (usually /tmp).
Click to reveal answer
intermediate
Why should you use mktemp instead of manually creating temporary files with fixed names?
Manually creating fixed-name temp files risks overwriting files or security issues. mktemp avoids these by making unique, safe names.
Click to reveal answer
beginner
How can you create a temporary directory using mktemp?
Use mktemp -d to create a unique temporary directory instead of a file.
Click to reveal answer
What does the XXXXXX part in the mktemp template represent?
AA placeholder replaced by random characters
BA fixed string in the filename
CThe file extension
DThe directory path
Which option creates a temporary directory with mktemp?
A-f
B-t
C-r
D-d
Why is using mktemp safer than creating temp files with fixed names?
AIt creates files faster
BIt automatically deletes files
CIt avoids overwriting and security risks
DIt uses less disk space
What will mktemp tempfile_XXXXXX do?
ACreate a file named exactly 'tempfile_XXXXXX'
BCreate a file with a unique name starting with 'tempfile_'
CCreate a directory named 'tempfile_XXXXXX'
DFail because of invalid syntax
If you want to create a temporary file in the system temp directory, which option do you use?
A-p /tmp
B-d /tmp
C-t /tmp
D-f /tmp
Explain how to safely create and use a temporary file in a bash script using mktemp.
Think about creating, using, and cleaning up the temp file.
You got /4 concepts.
    Describe the security benefits of using mktemp over fixed temporary file names.
    Consider what can go wrong with fixed names.
    You got /4 concepts.