Temporary folder that automatically destroyed after process exit
Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
Can we use temporary folders like temporary files
TMP=$(mktemp ... )
exec 3<>$TMP
rm $TMP
cat <&3
which will be destroyed automatically after this shell exit?
file-descriptors tmpfs
New contributor
Bob Johnson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
3
down vote
favorite
Can we use temporary folders like temporary files
TMP=$(mktemp ... )
exec 3<>$TMP
rm $TMP
cat <&3
which will be destroyed automatically after this shell exit?
file-descriptors tmpfs
New contributor
Bob Johnson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
Can we use temporary folders like temporary files
TMP=$(mktemp ... )
exec 3<>$TMP
rm $TMP
cat <&3
which will be destroyed automatically after this shell exit?
file-descriptors tmpfs
New contributor
Bob Johnson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Can we use temporary folders like temporary files
TMP=$(mktemp ... )
exec 3<>$TMP
rm $TMP
cat <&3
which will be destroyed automatically after this shell exit?
file-descriptors tmpfs
file-descriptors tmpfs
New contributor
Bob Johnson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Bob Johnson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Bob Johnson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 2 hours ago
Bob Johnson
241
241
New contributor
Bob Johnson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Bob Johnson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Bob Johnson is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
3
down vote
In the case of a temporary file, your example in the question would create it, then unlink it from the directory (making it "disappear"), and when the script closes the filedescriptor (probably upon termination), the space taken by the file would be reclaimable by the system. This is a common way to deal with temporary files in languages like C.
It is, as far as I know, not possible to open a directory in the same way in the shell. The code examples given below avoids having to juggle filedescriptors completely.
A common way to delete temporary files and directories at the termination of a script is by installing a cleanup EXIT
trap:
tmpdir=$(mktemp -d)
tmpfile=$(mktemp)
trap 'rm -f "$tmpfile"; rm -rf "$tmpdir"' EXIT
# The rest of the script goes here.
Or you may call a cleanup function:
cleanup ()
rm -f "$tmpfile"
rm -rf "$tmpdir"
tmpdir=$(mktemp -d)
tmpfile=$(mktemp)
trap cleanup EXIT
# The rest of the script goes here.
add a comment |Â
up vote
1
down vote
Write a shell-function that will be executed when your script if finished. In the example below I call it 'cleanup' and set a trap to be executed on exit levels 1 2 3 6
trap cleanup 0 1 2 3 6
cleanup()
[ -d $TMP ] && rm -rf $TMP
See this post for more info.
add a comment |Â
up vote
1
down vote
You can chdir into it and then remove it, provided that you don't try to use paths inside it afterwards:
#! /bin/sh
dir=`mktemp -d`
cd "$dir"
exec 4>file 3<file
rm -fr "$dir"
echo yes >&4 # OK
cat <&3 # OK
cat file # FAIL
echo yes > file # FAIL
I haven't checked, but it's most probably the same problem when using openat(2) in C with a directory that no longer exists in the file system.
If you're root, you can play with a separate namespace, and mount -t tmpfs tmpfs /dir
inside it.
The cannonical answers (set a trap on EXIT) don't work if your script is forced into an unclean exit (eg. with SIGKILL); that may leave sensitive data hanging around.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
In the case of a temporary file, your example in the question would create it, then unlink it from the directory (making it "disappear"), and when the script closes the filedescriptor (probably upon termination), the space taken by the file would be reclaimable by the system. This is a common way to deal with temporary files in languages like C.
It is, as far as I know, not possible to open a directory in the same way in the shell. The code examples given below avoids having to juggle filedescriptors completely.
A common way to delete temporary files and directories at the termination of a script is by installing a cleanup EXIT
trap:
tmpdir=$(mktemp -d)
tmpfile=$(mktemp)
trap 'rm -f "$tmpfile"; rm -rf "$tmpdir"' EXIT
# The rest of the script goes here.
Or you may call a cleanup function:
cleanup ()
rm -f "$tmpfile"
rm -rf "$tmpdir"
tmpdir=$(mktemp -d)
tmpfile=$(mktemp)
trap cleanup EXIT
# The rest of the script goes here.
add a comment |Â
up vote
3
down vote
In the case of a temporary file, your example in the question would create it, then unlink it from the directory (making it "disappear"), and when the script closes the filedescriptor (probably upon termination), the space taken by the file would be reclaimable by the system. This is a common way to deal with temporary files in languages like C.
It is, as far as I know, not possible to open a directory in the same way in the shell. The code examples given below avoids having to juggle filedescriptors completely.
A common way to delete temporary files and directories at the termination of a script is by installing a cleanup EXIT
trap:
tmpdir=$(mktemp -d)
tmpfile=$(mktemp)
trap 'rm -f "$tmpfile"; rm -rf "$tmpdir"' EXIT
# The rest of the script goes here.
Or you may call a cleanup function:
cleanup ()
rm -f "$tmpfile"
rm -rf "$tmpdir"
tmpdir=$(mktemp -d)
tmpfile=$(mktemp)
trap cleanup EXIT
# The rest of the script goes here.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
In the case of a temporary file, your example in the question would create it, then unlink it from the directory (making it "disappear"), and when the script closes the filedescriptor (probably upon termination), the space taken by the file would be reclaimable by the system. This is a common way to deal with temporary files in languages like C.
It is, as far as I know, not possible to open a directory in the same way in the shell. The code examples given below avoids having to juggle filedescriptors completely.
A common way to delete temporary files and directories at the termination of a script is by installing a cleanup EXIT
trap:
tmpdir=$(mktemp -d)
tmpfile=$(mktemp)
trap 'rm -f "$tmpfile"; rm -rf "$tmpdir"' EXIT
# The rest of the script goes here.
Or you may call a cleanup function:
cleanup ()
rm -f "$tmpfile"
rm -rf "$tmpdir"
tmpdir=$(mktemp -d)
tmpfile=$(mktemp)
trap cleanup EXIT
# The rest of the script goes here.
In the case of a temporary file, your example in the question would create it, then unlink it from the directory (making it "disappear"), and when the script closes the filedescriptor (probably upon termination), the space taken by the file would be reclaimable by the system. This is a common way to deal with temporary files in languages like C.
It is, as far as I know, not possible to open a directory in the same way in the shell. The code examples given below avoids having to juggle filedescriptors completely.
A common way to delete temporary files and directories at the termination of a script is by installing a cleanup EXIT
trap:
tmpdir=$(mktemp -d)
tmpfile=$(mktemp)
trap 'rm -f "$tmpfile"; rm -rf "$tmpdir"' EXIT
# The rest of the script goes here.
Or you may call a cleanup function:
cleanup ()
rm -f "$tmpfile"
rm -rf "$tmpdir"
tmpdir=$(mktemp -d)
tmpfile=$(mktemp)
trap cleanup EXIT
# The rest of the script goes here.
edited 1 hour ago
answered 2 hours ago


Kusalananda
113k15217345
113k15217345
add a comment |Â
add a comment |Â
up vote
1
down vote
Write a shell-function that will be executed when your script if finished. In the example below I call it 'cleanup' and set a trap to be executed on exit levels 1 2 3 6
trap cleanup 0 1 2 3 6
cleanup()
[ -d $TMP ] && rm -rf $TMP
See this post for more info.
add a comment |Â
up vote
1
down vote
Write a shell-function that will be executed when your script if finished. In the example below I call it 'cleanup' and set a trap to be executed on exit levels 1 2 3 6
trap cleanup 0 1 2 3 6
cleanup()
[ -d $TMP ] && rm -rf $TMP
See this post for more info.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Write a shell-function that will be executed when your script if finished. In the example below I call it 'cleanup' and set a trap to be executed on exit levels 1 2 3 6
trap cleanup 0 1 2 3 6
cleanup()
[ -d $TMP ] && rm -rf $TMP
See this post for more info.
Write a shell-function that will be executed when your script if finished. In the example below I call it 'cleanup' and set a trap to be executed on exit levels 1 2 3 6
trap cleanup 0 1 2 3 6
cleanup()
[ -d $TMP ] && rm -rf $TMP
See this post for more info.
answered 2 hours ago
Dirk Krijgsman
4546
4546
add a comment |Â
add a comment |Â
up vote
1
down vote
You can chdir into it and then remove it, provided that you don't try to use paths inside it afterwards:
#! /bin/sh
dir=`mktemp -d`
cd "$dir"
exec 4>file 3<file
rm -fr "$dir"
echo yes >&4 # OK
cat <&3 # OK
cat file # FAIL
echo yes > file # FAIL
I haven't checked, but it's most probably the same problem when using openat(2) in C with a directory that no longer exists in the file system.
If you're root, you can play with a separate namespace, and mount -t tmpfs tmpfs /dir
inside it.
The cannonical answers (set a trap on EXIT) don't work if your script is forced into an unclean exit (eg. with SIGKILL); that may leave sensitive data hanging around.
add a comment |Â
up vote
1
down vote
You can chdir into it and then remove it, provided that you don't try to use paths inside it afterwards:
#! /bin/sh
dir=`mktemp -d`
cd "$dir"
exec 4>file 3<file
rm -fr "$dir"
echo yes >&4 # OK
cat <&3 # OK
cat file # FAIL
echo yes > file # FAIL
I haven't checked, but it's most probably the same problem when using openat(2) in C with a directory that no longer exists in the file system.
If you're root, you can play with a separate namespace, and mount -t tmpfs tmpfs /dir
inside it.
The cannonical answers (set a trap on EXIT) don't work if your script is forced into an unclean exit (eg. with SIGKILL); that may leave sensitive data hanging around.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You can chdir into it and then remove it, provided that you don't try to use paths inside it afterwards:
#! /bin/sh
dir=`mktemp -d`
cd "$dir"
exec 4>file 3<file
rm -fr "$dir"
echo yes >&4 # OK
cat <&3 # OK
cat file # FAIL
echo yes > file # FAIL
I haven't checked, but it's most probably the same problem when using openat(2) in C with a directory that no longer exists in the file system.
If you're root, you can play with a separate namespace, and mount -t tmpfs tmpfs /dir
inside it.
The cannonical answers (set a trap on EXIT) don't work if your script is forced into an unclean exit (eg. with SIGKILL); that may leave sensitive data hanging around.
You can chdir into it and then remove it, provided that you don't try to use paths inside it afterwards:
#! /bin/sh
dir=`mktemp -d`
cd "$dir"
exec 4>file 3<file
rm -fr "$dir"
echo yes >&4 # OK
cat <&3 # OK
cat file # FAIL
echo yes > file # FAIL
I haven't checked, but it's most probably the same problem when using openat(2) in C with a directory that no longer exists in the file system.
If you're root, you can play with a separate namespace, and mount -t tmpfs tmpfs /dir
inside it.
The cannonical answers (set a trap on EXIT) don't work if your script is forced into an unclean exit (eg. with SIGKILL); that may leave sensitive data hanging around.
edited 17 mins ago
answered 59 mins ago
qubert
3955
3955
add a comment |Â
add a comment |Â
Bob Johnson is a new contributor. Be nice, and check out our Code of Conduct.
Bob Johnson is a new contributor. Be nice, and check out our Code of Conduct.
Bob Johnson is a new contributor. Be nice, and check out our Code of Conduct.
Bob Johnson is a new contributor. Be nice, and check out our Code of Conduct.
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%2f480330%2ftemporary-folder-that-automatically-destroyed-after-process-exit%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