You're developing a Bash script that needs to create a temporary directory, perform some operations within it, and then delete the temporary directory and its contents when done. Which command sequence ensures the secure creation and deletion of the temporary directory?1.0 Markstmp_dir="/tmp/mytempdir"mkdir $tmp_dirrm -r $tmp_dirtmp_dir=$(mktemp -d)rm -r $tmp_dirtmp_dir="/tmp/mytempdir"trap "rm -r $tmp_dir" EXITmkdir $tmp_dirtmp_dir=$(mktemp -d)trap "rm -r $tmp_dir" EXITtmp_dir="/tmp/mytempdir"mkdir $tmp_dirrm -rf $tmp_dir
Question
You're developing a Bash script that needs to create a temporary directory, perform some operations within it, and then delete the temporary directory and its contents when done. Which command sequence ensures the secure creation and deletion of the temporary directory?1.0 Markstmp_dir="/tmp/mytempdir"mkdir tmp_dirtmp_dir=tmp_dirtmp_dir="/tmp/mytempdir"trap "rm -r tmp_dirtmp_dir=tmp_dir" EXITtmp_dir="/tmp/mytempdir"mkdir tmp_dir
Solution
The correct command sequence for securely creating and deleting a temporary directory in a Bash script is:
tmp_dir=$(mktemp -d)
trap "rm -r $tmp_dir" EXIT
Here's a step-by-step explanation:
-
tmp_dir=$(mktemp -d): This command creates a new temporary directory. Themktemp -dcommand creates a unique temporary directory and returns its name. The$(...)syntax is a command substitution, meaning it runs the command inside the parentheses and replaces the command with its output. So this line is creating a new temporary directory and storing its name in thetmp_dirvariable. -
trap "rm -r $tmp_dir" EXIT: Thetrapcommand allows you to specify commands that will be executed when a certain signal is received. In this case, the signal isEXIT, which is sent when the script exits for any reason. The commandrm -r $tmp_dirwill delete the temporary directory and its contents. So this line is setting up a trap that will delete the temporary directory whenever the script exits.
Similar Questions
What command is used to remove a directory?
What command is used to remove the directory in Linux?(1 Point)rdrdirrmdirremove
Which command is suitable in deleting directory that contains files in it?2.0 Marks#rm –r directory#rm directory#remove directory#delete directory
Identify the command which is used to remove a directory0.5 Marksrm -frmdeleterm -R
Explain the following Linux commands:(1) mkdir(2) touch(3) cat(4) rm
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.