Is there a programmatical difference between directories created with mktemp -d or mkdir?
Clash Royale CLAN TAG#URR8PPP
up vote
2
down vote
favorite
I try to understand Stephen Kitt's answer to this question where he created a temporary directory with the following code:
#!/bin/bash
scripttmp=$(mktemp -d) # Create a temporary directory (these will usually be created under /tmp or /var/tmp/)
Each time I run this command I see a new temporary directory created under /tmp/
(I didn't know it will appear there until reading Roaima's answer here):
IIUC, there is no programmatical difference between a regular directory to a temporary directory (the only difference is in how these directories are used, by means of the time each one stays on the machine).
If there is no programmatical difference, why should one prefer mktemp -d
over the more minimal mkdir
?
directory mkdir mktemp
add a comment |Â
up vote
2
down vote
favorite
I try to understand Stephen Kitt's answer to this question where he created a temporary directory with the following code:
#!/bin/bash
scripttmp=$(mktemp -d) # Create a temporary directory (these will usually be created under /tmp or /var/tmp/)
Each time I run this command I see a new temporary directory created under /tmp/
(I didn't know it will appear there until reading Roaima's answer here):
IIUC, there is no programmatical difference between a regular directory to a temporary directory (the only difference is in how these directories are used, by means of the time each one stays on the machine).
If there is no programmatical difference, why should one prefer mktemp -d
over the more minimal mkdir
?
directory mkdir mktemp
I think the answer to this question on Stack Overflow explains it very well. Basically, you want to usemktemp
when added security matters.
â Mr Shunz
6 hours ago
@JohnDoea, I foundmktemp -d
easier to use.
â prosti
44 mins ago
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I try to understand Stephen Kitt's answer to this question where he created a temporary directory with the following code:
#!/bin/bash
scripttmp=$(mktemp -d) # Create a temporary directory (these will usually be created under /tmp or /var/tmp/)
Each time I run this command I see a new temporary directory created under /tmp/
(I didn't know it will appear there until reading Roaima's answer here):
IIUC, there is no programmatical difference between a regular directory to a temporary directory (the only difference is in how these directories are used, by means of the time each one stays on the machine).
If there is no programmatical difference, why should one prefer mktemp -d
over the more minimal mkdir
?
directory mkdir mktemp
I try to understand Stephen Kitt's answer to this question where he created a temporary directory with the following code:
#!/bin/bash
scripttmp=$(mktemp -d) # Create a temporary directory (these will usually be created under /tmp or /var/tmp/)
Each time I run this command I see a new temporary directory created under /tmp/
(I didn't know it will appear there until reading Roaima's answer here):
IIUC, there is no programmatical difference between a regular directory to a temporary directory (the only difference is in how these directories are used, by means of the time each one stays on the machine).
If there is no programmatical difference, why should one prefer mktemp -d
over the more minimal mkdir
?
directory mkdir mktemp
directory mkdir mktemp
edited 42 mins ago
asked 6 hours ago
JohnDoea
155726
155726
I think the answer to this question on Stack Overflow explains it very well. Basically, you want to usemktemp
when added security matters.
â Mr Shunz
6 hours ago
@JohnDoea, I foundmktemp -d
easier to use.
â prosti
44 mins ago
add a comment |Â
I think the answer to this question on Stack Overflow explains it very well. Basically, you want to usemktemp
when added security matters.
â Mr Shunz
6 hours ago
@JohnDoea, I foundmktemp -d
easier to use.
â prosti
44 mins ago
I think the answer to this question on Stack Overflow explains it very well. Basically, you want to use
mktemp
when added security matters.â Mr Shunz
6 hours ago
I think the answer to this question on Stack Overflow explains it very well. Basically, you want to use
mktemp
when added security matters.â Mr Shunz
6 hours ago
@JohnDoea, I found
mktemp -d
easier to use.â prosti
44 mins ago
@JohnDoea, I found
mktemp -d
easier to use.â prosti
44 mins ago
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
5
down vote
accepted
When using mkdir
, the script would have to make sure that it creates a directory with a name that does not already exist. It would be an error to use mkdir dirname
if dirname
is an existing name in the current directory.
When creating a temporary directory (i.e. a directory that is not needed much longer than during the lifetime of the current script), the name of the directory is not important, and mktemp -d
will find a name that is not already taken by something else.
mktemp -d
makes it easier and more secure to create a temporary directory. Without mktemp -d
, one would have to try to mkdir
with several names until one succeeded. This is both unnecessarily complicated and can be done wrongly (possibly introducing subtle race conditions in the code).
mktemp
also gives the user of the script a bit of control in where they want the temporary directory to be created. If the script, for example, produces a massive amount of temporary data that has to be stored in that directory, the user may set the TMPDIR
environment variable (before or as they are invoking the script) to point to a writable directory on a partition where there is enough space available. mktemp -d
would then create the temporary directory beneath that path.
add a comment |Â
up vote
1
down vote
The thing with mktemp -d
since it creates a temporary directory with a random name is:
You need to remember to delete the directory after using it, if you have a strict temp directory deleting policy.
This is why you wrote scripttmp=$(mktemp -d)
to hold the value. In general it is easy to use.
Unless you don't care, you may use this little example to generate the random name and remove the folder later:
var=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 20 | head -n 1);
mkdir "/tmp/$var"
#some code
rm -rf "/tmp/$var"
Is there a programmatical difference between directories created with mktemp -d or mkdir?
Well there is. With mkdir
you generate the directory name, and with mktemp -d
the system creates the directory for you based on /dev/urandom
gettimeofday
, getpid
, ... and other functions to gain the name uniqueness.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
When using mkdir
, the script would have to make sure that it creates a directory with a name that does not already exist. It would be an error to use mkdir dirname
if dirname
is an existing name in the current directory.
When creating a temporary directory (i.e. a directory that is not needed much longer than during the lifetime of the current script), the name of the directory is not important, and mktemp -d
will find a name that is not already taken by something else.
mktemp -d
makes it easier and more secure to create a temporary directory. Without mktemp -d
, one would have to try to mkdir
with several names until one succeeded. This is both unnecessarily complicated and can be done wrongly (possibly introducing subtle race conditions in the code).
mktemp
also gives the user of the script a bit of control in where they want the temporary directory to be created. If the script, for example, produces a massive amount of temporary data that has to be stored in that directory, the user may set the TMPDIR
environment variable (before or as they are invoking the script) to point to a writable directory on a partition where there is enough space available. mktemp -d
would then create the temporary directory beneath that path.
add a comment |Â
up vote
5
down vote
accepted
When using mkdir
, the script would have to make sure that it creates a directory with a name that does not already exist. It would be an error to use mkdir dirname
if dirname
is an existing name in the current directory.
When creating a temporary directory (i.e. a directory that is not needed much longer than during the lifetime of the current script), the name of the directory is not important, and mktemp -d
will find a name that is not already taken by something else.
mktemp -d
makes it easier and more secure to create a temporary directory. Without mktemp -d
, one would have to try to mkdir
with several names until one succeeded. This is both unnecessarily complicated and can be done wrongly (possibly introducing subtle race conditions in the code).
mktemp
also gives the user of the script a bit of control in where they want the temporary directory to be created. If the script, for example, produces a massive amount of temporary data that has to be stored in that directory, the user may set the TMPDIR
environment variable (before or as they are invoking the script) to point to a writable directory on a partition where there is enough space available. mktemp -d
would then create the temporary directory beneath that path.
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
When using mkdir
, the script would have to make sure that it creates a directory with a name that does not already exist. It would be an error to use mkdir dirname
if dirname
is an existing name in the current directory.
When creating a temporary directory (i.e. a directory that is not needed much longer than during the lifetime of the current script), the name of the directory is not important, and mktemp -d
will find a name that is not already taken by something else.
mktemp -d
makes it easier and more secure to create a temporary directory. Without mktemp -d
, one would have to try to mkdir
with several names until one succeeded. This is both unnecessarily complicated and can be done wrongly (possibly introducing subtle race conditions in the code).
mktemp
also gives the user of the script a bit of control in where they want the temporary directory to be created. If the script, for example, produces a massive amount of temporary data that has to be stored in that directory, the user may set the TMPDIR
environment variable (before or as they are invoking the script) to point to a writable directory on a partition where there is enough space available. mktemp -d
would then create the temporary directory beneath that path.
When using mkdir
, the script would have to make sure that it creates a directory with a name that does not already exist. It would be an error to use mkdir dirname
if dirname
is an existing name in the current directory.
When creating a temporary directory (i.e. a directory that is not needed much longer than during the lifetime of the current script), the name of the directory is not important, and mktemp -d
will find a name that is not already taken by something else.
mktemp -d
makes it easier and more secure to create a temporary directory. Without mktemp -d
, one would have to try to mkdir
with several names until one succeeded. This is both unnecessarily complicated and can be done wrongly (possibly introducing subtle race conditions in the code).
mktemp
also gives the user of the script a bit of control in where they want the temporary directory to be created. If the script, for example, produces a massive amount of temporary data that has to be stored in that directory, the user may set the TMPDIR
environment variable (before or as they are invoking the script) to point to a writable directory on a partition where there is enough space available. mktemp -d
would then create the temporary directory beneath that path.
edited 5 hours ago
answered 6 hours ago
Kusalananda
107k14209331
107k14209331
add a comment |Â
add a comment |Â
up vote
1
down vote
The thing with mktemp -d
since it creates a temporary directory with a random name is:
You need to remember to delete the directory after using it, if you have a strict temp directory deleting policy.
This is why you wrote scripttmp=$(mktemp -d)
to hold the value. In general it is easy to use.
Unless you don't care, you may use this little example to generate the random name and remove the folder later:
var=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 20 | head -n 1);
mkdir "/tmp/$var"
#some code
rm -rf "/tmp/$var"
Is there a programmatical difference between directories created with mktemp -d or mkdir?
Well there is. With mkdir
you generate the directory name, and with mktemp -d
the system creates the directory for you based on /dev/urandom
gettimeofday
, getpid
, ... and other functions to gain the name uniqueness.
add a comment |Â
up vote
1
down vote
The thing with mktemp -d
since it creates a temporary directory with a random name is:
You need to remember to delete the directory after using it, if you have a strict temp directory deleting policy.
This is why you wrote scripttmp=$(mktemp -d)
to hold the value. In general it is easy to use.
Unless you don't care, you may use this little example to generate the random name and remove the folder later:
var=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 20 | head -n 1);
mkdir "/tmp/$var"
#some code
rm -rf "/tmp/$var"
Is there a programmatical difference between directories created with mktemp -d or mkdir?
Well there is. With mkdir
you generate the directory name, and with mktemp -d
the system creates the directory for you based on /dev/urandom
gettimeofday
, getpid
, ... and other functions to gain the name uniqueness.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
The thing with mktemp -d
since it creates a temporary directory with a random name is:
You need to remember to delete the directory after using it, if you have a strict temp directory deleting policy.
This is why you wrote scripttmp=$(mktemp -d)
to hold the value. In general it is easy to use.
Unless you don't care, you may use this little example to generate the random name and remove the folder later:
var=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 20 | head -n 1);
mkdir "/tmp/$var"
#some code
rm -rf "/tmp/$var"
Is there a programmatical difference between directories created with mktemp -d or mkdir?
Well there is. With mkdir
you generate the directory name, and with mktemp -d
the system creates the directory for you based on /dev/urandom
gettimeofday
, getpid
, ... and other functions to gain the name uniqueness.
The thing with mktemp -d
since it creates a temporary directory with a random name is:
You need to remember to delete the directory after using it, if you have a strict temp directory deleting policy.
This is why you wrote scripttmp=$(mktemp -d)
to hold the value. In general it is easy to use.
Unless you don't care, you may use this little example to generate the random name and remove the folder later:
var=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 20 | head -n 1);
mkdir "/tmp/$var"
#some code
rm -rf "/tmp/$var"
Is there a programmatical difference between directories created with mktemp -d or mkdir?
Well there is. With mkdir
you generate the directory name, and with mktemp -d
the system creates the directory for you based on /dev/urandom
gettimeofday
, getpid
, ... and other functions to gain the name uniqueness.
edited 47 mins ago
answered 6 hours ago
prosti
20013
20013
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f471589%2fis-there-a-programmatical-difference-between-directories-created-with-mktemp-d%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I think the answer to this question on Stack Overflow explains it very well. Basically, you want to use
mktemp
when added security matters.â Mr Shunz
6 hours ago
@JohnDoea, I found
mktemp -d
easier to use.â prosti
44 mins ago