Bird
0
0

How can you create a secure temporary directory inside /var/tmp using mktemp in bash?

hard🚀 Application Q9 of 15
Bash Scripting - File Operations in Scripts
How can you create a secure temporary directory inside /var/tmp using mktemp in bash?
Atempdir=$(mktemp -d /var/tmp/tmp.XXXXXX)
Btempdir=$(mktemp -d /var/tmp)
Ctempdir=$(mktemp /var/tmp/tmp.XXXXXX)
Dtempdir=$(mktemp -f /var/tmp/tmp.XXXXXX)
Step-by-Step Solution
Solution:
  1. Step 1: Use -d to create directory

    The -d option creates a directory instead of a file.
  2. Step 2: Specify template with XXXXXX

    The template /var/tmp/tmp.XXXXXX ensures a unique directory name inside /var/tmp.
  3. Step 3: Analyze other options

    tempdir=$(mktemp -d /var/tmp) lacks the template and will fail. tempdir=$(mktemp /var/tmp/tmp.XXXXXX) creates a file, not a directory. tempdir=$(mktemp -f /var/tmp/tmp.XXXXXX) uses invalid -f flag.
  4. Final Answer:

    tempdir=$(mktemp -d /var/tmp/tmp.XXXXXX) -> Option A
  5. Quick Check:

    Does the command create a unique temp directory in /var/tmp? Yes. [OK]
Quick Trick: Use mktemp -d with full path and XXXXXX template [OK]
Common Mistakes:
MISTAKES
  • Omitting the template with XXXXXX
  • Using wrong flags or creating files instead of directories

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes