Unzip to a folder with the same name [duplicate]
up vote
0
down vote
favorite
This question already has an answer here:
How can I tell if “unzip” will create a single folder ahead of time?
3 answers
How to unzip a file (ex: foo.zip
) to a folder with the same name (foo/
)?
Basically, I want to create an alias of unzip that unzips files into a folder with the same name (instead of the current folder). That's how Mac's unzip utility works and I want to achieve the same in CLI.
I found a bunch of questions about how to unzip multiple files into folders with the same name, but surprisingly, I could not find an answer to the simpler question above.
directory zip
marked as duplicate by Christopher, Kusalananda, Rui F Ribeiro, Fabby, Anthony Geoghegan 23 hours ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
0
down vote
favorite
This question already has an answer here:
How can I tell if “unzip” will create a single folder ahead of time?
3 answers
How to unzip a file (ex: foo.zip
) to a folder with the same name (foo/
)?
Basically, I want to create an alias of unzip that unzips files into a folder with the same name (instead of the current folder). That's how Mac's unzip utility works and I want to achieve the same in CLI.
I found a bunch of questions about how to unzip multiple files into folders with the same name, but surprisingly, I could not find an answer to the simpler question above.
directory zip
marked as duplicate by Christopher, Kusalananda, Rui F Ribeiro, Fabby, Anthony Geoghegan 23 hours ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
@Christopher I don't see how it's a duplicate. Can you find an answer to my question in that question?
– Emanuil Rusev
yesterday
1
The accepted anwer answers your question:unzip -d foo foo.zip
.
– Fabby
yesterday
@Fabby, no it doesn't, I need a dynamic solution that won't require adding the name of the target folder. I believe the second paragraph makes this very clear.
– Emanuil Rusev
15 hours ago
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
How can I tell if “unzip” will create a single folder ahead of time?
3 answers
How to unzip a file (ex: foo.zip
) to a folder with the same name (foo/
)?
Basically, I want to create an alias of unzip that unzips files into a folder with the same name (instead of the current folder). That's how Mac's unzip utility works and I want to achieve the same in CLI.
I found a bunch of questions about how to unzip multiple files into folders with the same name, but surprisingly, I could not find an answer to the simpler question above.
directory zip
This question already has an answer here:
How can I tell if “unzip” will create a single folder ahead of time?
3 answers
How to unzip a file (ex: foo.zip
) to a folder with the same name (foo/
)?
Basically, I want to create an alias of unzip that unzips files into a folder with the same name (instead of the current folder). That's how Mac's unzip utility works and I want to achieve the same in CLI.
I found a bunch of questions about how to unzip multiple files into folders with the same name, but surprisingly, I could not find an answer to the simpler question above.
This question already has an answer here:
How can I tell if “unzip” will create a single folder ahead of time?
3 answers
directory zip
directory zip
edited yesterday
asked yesterday
Emanuil Rusev
1106
1106
marked as duplicate by Christopher, Kusalananda, Rui F Ribeiro, Fabby, Anthony Geoghegan 23 hours ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Christopher, Kusalananda, Rui F Ribeiro, Fabby, Anthony Geoghegan 23 hours ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
@Christopher I don't see how it's a duplicate. Can you find an answer to my question in that question?
– Emanuil Rusev
yesterday
1
The accepted anwer answers your question:unzip -d foo foo.zip
.
– Fabby
yesterday
@Fabby, no it doesn't, I need a dynamic solution that won't require adding the name of the target folder. I believe the second paragraph makes this very clear.
– Emanuil Rusev
15 hours ago
add a comment |
@Christopher I don't see how it's a duplicate. Can you find an answer to my question in that question?
– Emanuil Rusev
yesterday
1
The accepted anwer answers your question:unzip -d foo foo.zip
.
– Fabby
yesterday
@Fabby, no it doesn't, I need a dynamic solution that won't require adding the name of the target folder. I believe the second paragraph makes this very clear.
– Emanuil Rusev
15 hours ago
@Christopher I don't see how it's a duplicate. Can you find an answer to my question in that question?
– Emanuil Rusev
yesterday
@Christopher I don't see how it's a duplicate. Can you find an answer to my question in that question?
– Emanuil Rusev
yesterday
1
1
The accepted anwer answers your question:
unzip -d foo foo.zip
.– Fabby
yesterday
The accepted anwer answers your question:
unzip -d foo foo.zip
.– Fabby
yesterday
@Fabby, no it doesn't, I need a dynamic solution that won't require adding the name of the target folder. I believe the second paragraph makes this very clear.
– Emanuil Rusev
15 hours ago
@Fabby, no it doesn't, I need a dynamic solution that won't require adding the name of the target folder. I believe the second paragraph makes this very clear.
– Emanuil Rusev
15 hours ago
add a comment |
2 Answers
2
active
oldest
votes
up vote
3
down vote
accepted
I use unar
for this; by default, if an archive contains more than one top-level file or directory, it creates a directory to store the extracted contents, named after the archive in the way you describe:
unar foo.zip
You can force the creation of a directory in all cases with the -d
option:
unar -d foo.zip
Alternatively, a function can do this with unzip
:
unzd() {
if [[ $# != 1 ]]; then echo I need a single argument, the name of the archive to extract; return 1; fi
target="${1%.zip}"
unzip "$1" -d "${target##*/}"
}
The
target=${1%.zip}
line removes the .zip
extension, with no regard for anything else (so foo.zip
becomes foo
, and ~/foo.zip
becomes ~/foo
). The
${target##*/}
expansion removes anything up to the last /
, so ~/foo
becomes foo
. This means that the function extracts any .zip
file to a directory named after it, in the current directory. Use unzip $1 -d "${target}"
if you want to extract the archive to a directory alongside it instead.
Awesome, what's the meaning of##*/
part of the last line of the function? Thanks!
– Emanuil Rusev
yesterday
Wouldn't it make more sense to do the trimming of everything up to the last / in the same line that removes the.zip
. I'm new to bash so forgive me if the question is not very smart.
– Emanuil Rusev
yesterday
1
It would make more sense, unfortunately there’s no direct way to do so using the shell’s operators.
– Stephen Kitt
yesterday
Last question, what's that##*
syntax called? I'd like to search for it and learn more.
– Emanuil Rusev
yesterday
1
It’s part of parameter expansion; see this section of the Bash manual.
– Stephen Kitt
yesterday
add a comment |
up vote
5
down vote
Use unzip -d exdir zipfile.zip to extract a zipfile into a particular directory. In principle from reading your post literally you could write a function like this:
unzip_d () {
unzip -d "$1" "$1"
}
Since you want the .zip extension removed though, you can use special variable syntax to do that:
unzip_d () {
zipfile="$1"
zipdir=${1%.zip}
unzip -d "$zipdir" "$zipfile"
}
New contributor
additional tip: use basename to make it more robust. (already upvoted though) **;-)
– Fabby
13 hours ago
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
I use unar
for this; by default, if an archive contains more than one top-level file or directory, it creates a directory to store the extracted contents, named after the archive in the way you describe:
unar foo.zip
You can force the creation of a directory in all cases with the -d
option:
unar -d foo.zip
Alternatively, a function can do this with unzip
:
unzd() {
if [[ $# != 1 ]]; then echo I need a single argument, the name of the archive to extract; return 1; fi
target="${1%.zip}"
unzip "$1" -d "${target##*/}"
}
The
target=${1%.zip}
line removes the .zip
extension, with no regard for anything else (so foo.zip
becomes foo
, and ~/foo.zip
becomes ~/foo
). The
${target##*/}
expansion removes anything up to the last /
, so ~/foo
becomes foo
. This means that the function extracts any .zip
file to a directory named after it, in the current directory. Use unzip $1 -d "${target}"
if you want to extract the archive to a directory alongside it instead.
Awesome, what's the meaning of##*/
part of the last line of the function? Thanks!
– Emanuil Rusev
yesterday
Wouldn't it make more sense to do the trimming of everything up to the last / in the same line that removes the.zip
. I'm new to bash so forgive me if the question is not very smart.
– Emanuil Rusev
yesterday
1
It would make more sense, unfortunately there’s no direct way to do so using the shell’s operators.
– Stephen Kitt
yesterday
Last question, what's that##*
syntax called? I'd like to search for it and learn more.
– Emanuil Rusev
yesterday
1
It’s part of parameter expansion; see this section of the Bash manual.
– Stephen Kitt
yesterday
add a comment |
up vote
3
down vote
accepted
I use unar
for this; by default, if an archive contains more than one top-level file or directory, it creates a directory to store the extracted contents, named after the archive in the way you describe:
unar foo.zip
You can force the creation of a directory in all cases with the -d
option:
unar -d foo.zip
Alternatively, a function can do this with unzip
:
unzd() {
if [[ $# != 1 ]]; then echo I need a single argument, the name of the archive to extract; return 1; fi
target="${1%.zip}"
unzip "$1" -d "${target##*/}"
}
The
target=${1%.zip}
line removes the .zip
extension, with no regard for anything else (so foo.zip
becomes foo
, and ~/foo.zip
becomes ~/foo
). The
${target##*/}
expansion removes anything up to the last /
, so ~/foo
becomes foo
. This means that the function extracts any .zip
file to a directory named after it, in the current directory. Use unzip $1 -d "${target}"
if you want to extract the archive to a directory alongside it instead.
Awesome, what's the meaning of##*/
part of the last line of the function? Thanks!
– Emanuil Rusev
yesterday
Wouldn't it make more sense to do the trimming of everything up to the last / in the same line that removes the.zip
. I'm new to bash so forgive me if the question is not very smart.
– Emanuil Rusev
yesterday
1
It would make more sense, unfortunately there’s no direct way to do so using the shell’s operators.
– Stephen Kitt
yesterday
Last question, what's that##*
syntax called? I'd like to search for it and learn more.
– Emanuil Rusev
yesterday
1
It’s part of parameter expansion; see this section of the Bash manual.
– Stephen Kitt
yesterday
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
I use unar
for this; by default, if an archive contains more than one top-level file or directory, it creates a directory to store the extracted contents, named after the archive in the way you describe:
unar foo.zip
You can force the creation of a directory in all cases with the -d
option:
unar -d foo.zip
Alternatively, a function can do this with unzip
:
unzd() {
if [[ $# != 1 ]]; then echo I need a single argument, the name of the archive to extract; return 1; fi
target="${1%.zip}"
unzip "$1" -d "${target##*/}"
}
The
target=${1%.zip}
line removes the .zip
extension, with no regard for anything else (so foo.zip
becomes foo
, and ~/foo.zip
becomes ~/foo
). The
${target##*/}
expansion removes anything up to the last /
, so ~/foo
becomes foo
. This means that the function extracts any .zip
file to a directory named after it, in the current directory. Use unzip $1 -d "${target}"
if you want to extract the archive to a directory alongside it instead.
I use unar
for this; by default, if an archive contains more than one top-level file or directory, it creates a directory to store the extracted contents, named after the archive in the way you describe:
unar foo.zip
You can force the creation of a directory in all cases with the -d
option:
unar -d foo.zip
Alternatively, a function can do this with unzip
:
unzd() {
if [[ $# != 1 ]]; then echo I need a single argument, the name of the archive to extract; return 1; fi
target="${1%.zip}"
unzip "$1" -d "${target##*/}"
}
The
target=${1%.zip}
line removes the .zip
extension, with no regard for anything else (so foo.zip
becomes foo
, and ~/foo.zip
becomes ~/foo
). The
${target##*/}
expansion removes anything up to the last /
, so ~/foo
becomes foo
. This means that the function extracts any .zip
file to a directory named after it, in the current directory. Use unzip $1 -d "${target}"
if you want to extract the archive to a directory alongside it instead.
edited yesterday
answered yesterday
Stephen Kitt
162k24360438
162k24360438
Awesome, what's the meaning of##*/
part of the last line of the function? Thanks!
– Emanuil Rusev
yesterday
Wouldn't it make more sense to do the trimming of everything up to the last / in the same line that removes the.zip
. I'm new to bash so forgive me if the question is not very smart.
– Emanuil Rusev
yesterday
1
It would make more sense, unfortunately there’s no direct way to do so using the shell’s operators.
– Stephen Kitt
yesterday
Last question, what's that##*
syntax called? I'd like to search for it and learn more.
– Emanuil Rusev
yesterday
1
It’s part of parameter expansion; see this section of the Bash manual.
– Stephen Kitt
yesterday
add a comment |
Awesome, what's the meaning of##*/
part of the last line of the function? Thanks!
– Emanuil Rusev
yesterday
Wouldn't it make more sense to do the trimming of everything up to the last / in the same line that removes the.zip
. I'm new to bash so forgive me if the question is not very smart.
– Emanuil Rusev
yesterday
1
It would make more sense, unfortunately there’s no direct way to do so using the shell’s operators.
– Stephen Kitt
yesterday
Last question, what's that##*
syntax called? I'd like to search for it and learn more.
– Emanuil Rusev
yesterday
1
It’s part of parameter expansion; see this section of the Bash manual.
– Stephen Kitt
yesterday
Awesome, what's the meaning of
##*/
part of the last line of the function? Thanks!– Emanuil Rusev
yesterday
Awesome, what's the meaning of
##*/
part of the last line of the function? Thanks!– Emanuil Rusev
yesterday
Wouldn't it make more sense to do the trimming of everything up to the last / in the same line that removes the
.zip
. I'm new to bash so forgive me if the question is not very smart.– Emanuil Rusev
yesterday
Wouldn't it make more sense to do the trimming of everything up to the last / in the same line that removes the
.zip
. I'm new to bash so forgive me if the question is not very smart.– Emanuil Rusev
yesterday
1
1
It would make more sense, unfortunately there’s no direct way to do so using the shell’s operators.
– Stephen Kitt
yesterday
It would make more sense, unfortunately there’s no direct way to do so using the shell’s operators.
– Stephen Kitt
yesterday
Last question, what's that
##*
syntax called? I'd like to search for it and learn more.– Emanuil Rusev
yesterday
Last question, what's that
##*
syntax called? I'd like to search for it and learn more.– Emanuil Rusev
yesterday
1
1
It’s part of parameter expansion; see this section of the Bash manual.
– Stephen Kitt
yesterday
It’s part of parameter expansion; see this section of the Bash manual.
– Stephen Kitt
yesterday
add a comment |
up vote
5
down vote
Use unzip -d exdir zipfile.zip to extract a zipfile into a particular directory. In principle from reading your post literally you could write a function like this:
unzip_d () {
unzip -d "$1" "$1"
}
Since you want the .zip extension removed though, you can use special variable syntax to do that:
unzip_d () {
zipfile="$1"
zipdir=${1%.zip}
unzip -d "$zipdir" "$zipfile"
}
New contributor
additional tip: use basename to make it more robust. (already upvoted though) **;-)
– Fabby
13 hours ago
add a comment |
up vote
5
down vote
Use unzip -d exdir zipfile.zip to extract a zipfile into a particular directory. In principle from reading your post literally you could write a function like this:
unzip_d () {
unzip -d "$1" "$1"
}
Since you want the .zip extension removed though, you can use special variable syntax to do that:
unzip_d () {
zipfile="$1"
zipdir=${1%.zip}
unzip -d "$zipdir" "$zipfile"
}
New contributor
additional tip: use basename to make it more robust. (already upvoted though) **;-)
– Fabby
13 hours ago
add a comment |
up vote
5
down vote
up vote
5
down vote
Use unzip -d exdir zipfile.zip to extract a zipfile into a particular directory. In principle from reading your post literally you could write a function like this:
unzip_d () {
unzip -d "$1" "$1"
}
Since you want the .zip extension removed though, you can use special variable syntax to do that:
unzip_d () {
zipfile="$1"
zipdir=${1%.zip}
unzip -d "$zipdir" "$zipfile"
}
New contributor
Use unzip -d exdir zipfile.zip to extract a zipfile into a particular directory. In principle from reading your post literally you could write a function like this:
unzip_d () {
unzip -d "$1" "$1"
}
Since you want the .zip extension removed though, you can use special variable syntax to do that:
unzip_d () {
zipfile="$1"
zipdir=${1%.zip}
unzip -d "$zipdir" "$zipfile"
}
New contributor
edited yesterday
New contributor
answered yesterday
Silas Coker
962
962
New contributor
New contributor
additional tip: use basename to make it more robust. (already upvoted though) **;-)
– Fabby
13 hours ago
add a comment |
additional tip: use basename to make it more robust. (already upvoted though) **;-)
– Fabby
13 hours ago
additional tip: use basename to make it more robust. (already upvoted though) **;-)
– Fabby
13 hours ago
additional tip: use basename to make it more robust. (already upvoted though) **;-)
– Fabby
13 hours ago
add a comment |
@Christopher I don't see how it's a duplicate. Can you find an answer to my question in that question?
– Emanuil Rusev
yesterday
1
The accepted anwer answers your question:
unzip -d foo foo.zip
.– Fabby
yesterday
@Fabby, no it doesn't, I need a dynamic solution that won't require adding the name of the target folder. I believe the second paragraph makes this very clear.
– Emanuil Rusev
15 hours ago