How to know in advance if a .zip has a parent directory inside
Clash Royale CLAN TAG#URR8PPP
up vote
-2
down vote
favorite
On several occasions, say I have a.zip
, which unzips into a/*
itself.
However, often there are zip files which spill all their contents into the current directory. This requires me to manually create a director a
and unzip into it.
But if as a policy I always create a directory, and the zip turns out to be of the first kind, my structure looks like a/a/*
which is not ideal.
Is there a way to unzip a.zip
into a/*
irrespective of its type among the two types which I described above?
files filesystems zip
add a comment |Â
up vote
-2
down vote
favorite
On several occasions, say I have a.zip
, which unzips into a/*
itself.
However, often there are zip files which spill all their contents into the current directory. This requires me to manually create a director a
and unzip into it.
But if as a policy I always create a directory, and the zip turns out to be of the first kind, my structure looks like a/a/*
which is not ideal.
Is there a way to unzip a.zip
into a/*
irrespective of its type among the two types which I described above?
files filesystems zip
add a comment |Â
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
On several occasions, say I have a.zip
, which unzips into a/*
itself.
However, often there are zip files which spill all their contents into the current directory. This requires me to manually create a director a
and unzip into it.
But if as a policy I always create a directory, and the zip turns out to be of the first kind, my structure looks like a/a/*
which is not ideal.
Is there a way to unzip a.zip
into a/*
irrespective of its type among the two types which I described above?
files filesystems zip
On several occasions, say I have a.zip
, which unzips into a/*
itself.
However, often there are zip files which spill all their contents into the current directory. This requires me to manually create a director a
and unzip into it.
But if as a policy I always create a directory, and the zip turns out to be of the first kind, my structure looks like a/a/*
which is not ideal.
Is there a way to unzip a.zip
into a/*
irrespective of its type among the two types which I described above?
files filesystems zip
asked Nov 28 '17 at 10:04
Cheeku
993
993
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Try this:
archive="archive.zip"
has_parent=$(unzip -l "$archive" | tail -n+4 | head -n-2 | awk 'split($NF,a,"/");print a[1]' | sort -u | wc -l)
if test "$has_parent" -eq 1; then
unzip $archive
else
dir="./$(basename $archive%%.zip)"
mkdir "$dir"
unzip -d "$dir" $archive
fi
If using zipinfo
you can squeeze $has_parent
line to this:
has_parent=$(zipinfo -1 "$archive" | awk 'split($NF,a,"/");print a[1]' | sort -u | wc -l)
Idea is simple - if there are several files on the root level of the archive it obviously will trash your current directory if you unpack it, therefore you must create a parent directory beforehand and unpack files in there. Otherwise if all the files in the archive have the same parent then it's ok to unpack in the current dir.
add a comment |Â
up vote
1
down vote
Use zipinfo
for displaying, zip contents.:
$ zipinfo -1 a.zip
Though, this probably doesn't answer your question if you are looking for an automation answer.
Edit: What you could do is check each line of output from zipinfo
for a forward slash character. If there is a line missing the character, you know that it is located at the root of the zip file.
Unfortunately, I can't think of exactly how to do this of the top of my head. Not sure if it will work, but here is an untested command:
$ MKROOT=0
$ for X in $(zipinfo -1 a.zip); do $(echo "$X" | grep "/"); if test "$?" -gt "0"; then MKROOT=1; fi; done
$ if test "$MKROOT" -gt "0"; then mkdir "a"; cd "a"; unzip "../a.zip"; else unzip "a.zip"; fi
Edit: Mark Perryman's suggestion is probably better. I just realized that my command checks if files are contained in sub-folders, but it doesn't check if all are contained under the same root folder.
1
zipinfo -1 a.zip | sed -r 's#([^/]+/).*#1#' | sort -u | wc -l
will output 1 if all paths have the same leading directory.
â Mark Perryman
Nov 28 '17 at 10:47
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Try this:
archive="archive.zip"
has_parent=$(unzip -l "$archive" | tail -n+4 | head -n-2 | awk 'split($NF,a,"/");print a[1]' | sort -u | wc -l)
if test "$has_parent" -eq 1; then
unzip $archive
else
dir="./$(basename $archive%%.zip)"
mkdir "$dir"
unzip -d "$dir" $archive
fi
If using zipinfo
you can squeeze $has_parent
line to this:
has_parent=$(zipinfo -1 "$archive" | awk 'split($NF,a,"/");print a[1]' | sort -u | wc -l)
Idea is simple - if there are several files on the root level of the archive it obviously will trash your current directory if you unpack it, therefore you must create a parent directory beforehand and unpack files in there. Otherwise if all the files in the archive have the same parent then it's ok to unpack in the current dir.
add a comment |Â
up vote
1
down vote
accepted
Try this:
archive="archive.zip"
has_parent=$(unzip -l "$archive" | tail -n+4 | head -n-2 | awk 'split($NF,a,"/");print a[1]' | sort -u | wc -l)
if test "$has_parent" -eq 1; then
unzip $archive
else
dir="./$(basename $archive%%.zip)"
mkdir "$dir"
unzip -d "$dir" $archive
fi
If using zipinfo
you can squeeze $has_parent
line to this:
has_parent=$(zipinfo -1 "$archive" | awk 'split($NF,a,"/");print a[1]' | sort -u | wc -l)
Idea is simple - if there are several files on the root level of the archive it obviously will trash your current directory if you unpack it, therefore you must create a parent directory beforehand and unpack files in there. Otherwise if all the files in the archive have the same parent then it's ok to unpack in the current dir.
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Try this:
archive="archive.zip"
has_parent=$(unzip -l "$archive" | tail -n+4 | head -n-2 | awk 'split($NF,a,"/");print a[1]' | sort -u | wc -l)
if test "$has_parent" -eq 1; then
unzip $archive
else
dir="./$(basename $archive%%.zip)"
mkdir "$dir"
unzip -d "$dir" $archive
fi
If using zipinfo
you can squeeze $has_parent
line to this:
has_parent=$(zipinfo -1 "$archive" | awk 'split($NF,a,"/");print a[1]' | sort -u | wc -l)
Idea is simple - if there are several files on the root level of the archive it obviously will trash your current directory if you unpack it, therefore you must create a parent directory beforehand and unpack files in there. Otherwise if all the files in the archive have the same parent then it's ok to unpack in the current dir.
Try this:
archive="archive.zip"
has_parent=$(unzip -l "$archive" | tail -n+4 | head -n-2 | awk 'split($NF,a,"/");print a[1]' | sort -u | wc -l)
if test "$has_parent" -eq 1; then
unzip $archive
else
dir="./$(basename $archive%%.zip)"
mkdir "$dir"
unzip -d "$dir" $archive
fi
If using zipinfo
you can squeeze $has_parent
line to this:
has_parent=$(zipinfo -1 "$archive" | awk 'split($NF,a,"/");print a[1]' | sort -u | wc -l)
Idea is simple - if there are several files on the root level of the archive it obviously will trash your current directory if you unpack it, therefore you must create a parent directory beforehand and unpack files in there. Otherwise if all the files in the archive have the same parent then it's ok to unpack in the current dir.
edited Nov 28 '17 at 11:15
answered Nov 28 '17 at 10:50
Narà «nasK
8021618
8021618
add a comment |Â
add a comment |Â
up vote
1
down vote
Use zipinfo
for displaying, zip contents.:
$ zipinfo -1 a.zip
Though, this probably doesn't answer your question if you are looking for an automation answer.
Edit: What you could do is check each line of output from zipinfo
for a forward slash character. If there is a line missing the character, you know that it is located at the root of the zip file.
Unfortunately, I can't think of exactly how to do this of the top of my head. Not sure if it will work, but here is an untested command:
$ MKROOT=0
$ for X in $(zipinfo -1 a.zip); do $(echo "$X" | grep "/"); if test "$?" -gt "0"; then MKROOT=1; fi; done
$ if test "$MKROOT" -gt "0"; then mkdir "a"; cd "a"; unzip "../a.zip"; else unzip "a.zip"; fi
Edit: Mark Perryman's suggestion is probably better. I just realized that my command checks if files are contained in sub-folders, but it doesn't check if all are contained under the same root folder.
1
zipinfo -1 a.zip | sed -r 's#([^/]+/).*#1#' | sort -u | wc -l
will output 1 if all paths have the same leading directory.
â Mark Perryman
Nov 28 '17 at 10:47
add a comment |Â
up vote
1
down vote
Use zipinfo
for displaying, zip contents.:
$ zipinfo -1 a.zip
Though, this probably doesn't answer your question if you are looking for an automation answer.
Edit: What you could do is check each line of output from zipinfo
for a forward slash character. If there is a line missing the character, you know that it is located at the root of the zip file.
Unfortunately, I can't think of exactly how to do this of the top of my head. Not sure if it will work, but here is an untested command:
$ MKROOT=0
$ for X in $(zipinfo -1 a.zip); do $(echo "$X" | grep "/"); if test "$?" -gt "0"; then MKROOT=1; fi; done
$ if test "$MKROOT" -gt "0"; then mkdir "a"; cd "a"; unzip "../a.zip"; else unzip "a.zip"; fi
Edit: Mark Perryman's suggestion is probably better. I just realized that my command checks if files are contained in sub-folders, but it doesn't check if all are contained under the same root folder.
1
zipinfo -1 a.zip | sed -r 's#([^/]+/).*#1#' | sort -u | wc -l
will output 1 if all paths have the same leading directory.
â Mark Perryman
Nov 28 '17 at 10:47
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Use zipinfo
for displaying, zip contents.:
$ zipinfo -1 a.zip
Though, this probably doesn't answer your question if you are looking for an automation answer.
Edit: What you could do is check each line of output from zipinfo
for a forward slash character. If there is a line missing the character, you know that it is located at the root of the zip file.
Unfortunately, I can't think of exactly how to do this of the top of my head. Not sure if it will work, but here is an untested command:
$ MKROOT=0
$ for X in $(zipinfo -1 a.zip); do $(echo "$X" | grep "/"); if test "$?" -gt "0"; then MKROOT=1; fi; done
$ if test "$MKROOT" -gt "0"; then mkdir "a"; cd "a"; unzip "../a.zip"; else unzip "a.zip"; fi
Edit: Mark Perryman's suggestion is probably better. I just realized that my command checks if files are contained in sub-folders, but it doesn't check if all are contained under the same root folder.
Use zipinfo
for displaying, zip contents.:
$ zipinfo -1 a.zip
Though, this probably doesn't answer your question if you are looking for an automation answer.
Edit: What you could do is check each line of output from zipinfo
for a forward slash character. If there is a line missing the character, you know that it is located at the root of the zip file.
Unfortunately, I can't think of exactly how to do this of the top of my head. Not sure if it will work, but here is an untested command:
$ MKROOT=0
$ for X in $(zipinfo -1 a.zip); do $(echo "$X" | grep "/"); if test "$?" -gt "0"; then MKROOT=1; fi; done
$ if test "$MKROOT" -gt "0"; then mkdir "a"; cd "a"; unzip "../a.zip"; else unzip "a.zip"; fi
Edit: Mark Perryman's suggestion is probably better. I just realized that my command checks if files are contained in sub-folders, but it doesn't check if all are contained under the same root folder.
edited Nov 28 '17 at 11:07
answered Nov 28 '17 at 10:41
AntumDeluge
397
397
1
zipinfo -1 a.zip | sed -r 's#([^/]+/).*#1#' | sort -u | wc -l
will output 1 if all paths have the same leading directory.
â Mark Perryman
Nov 28 '17 at 10:47
add a comment |Â
1
zipinfo -1 a.zip | sed -r 's#([^/]+/).*#1#' | sort -u | wc -l
will output 1 if all paths have the same leading directory.
â Mark Perryman
Nov 28 '17 at 10:47
1
1
zipinfo -1 a.zip | sed -r 's#([^/]+/).*#1#' | sort -u | wc -l
will output 1 if all paths have the same leading directory.â Mark Perryman
Nov 28 '17 at 10:47
zipinfo -1 a.zip | sed -r 's#([^/]+/).*#1#' | sort -u | wc -l
will output 1 if all paths have the same leading directory.â Mark Perryman
Nov 28 '17 at 10:47
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%2f407453%2fhow-to-know-in-advance-if-a-zip-has-a-parent-directory-inside%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