- unzip a directory in unix [closed]
- 3 Answers 3
- Unzipping a folder without it’s full hierarchy
- Method #1
- Method #2
- Unzipping a folder with it’s full hierarchy
- Example
- Practical Examples of the Unzip Command in Linux
- Unzip command in Linux
- 1. Unzip to a directory
- 2. See the content of the zip file without extracting
- 3. Overwrite all the files without prompting
- 4. Do not overwrite any files -n
- 5. Update files and create if necessary
- 6. Freshen existing files but create none
- 7. Extract in quiet mode
- 8. Exclude files from extraction
- How to unzip into a given directory
- 1 Answer 1
- You must log in to answer this question.
- Related
- Hot Network Questions
- Subscribe to RSS
unzip a directory in unix [closed]
This is creating the root structure a/b/c/d/e/f/sample . I need to unzip a folder in the current directory.
Can you please try and explain this question a bit more? As it stands it’s very unclear what you’re asking. There are 3 ways to interpret what you’re saying. (1) unzip with no directories (2) blindly unzip just the contents of the folder without knowing it ahead of time, or (3) unzip a specific folder in the zip file.
3 Answers 3
-j junk paths. The archive's directory structure is not recreated; all files are deposited in the extraction directory (by default, the current one).
Note that this will unpack all the files into the current directory, regardless of the in-file directory structure. That is probably not what the OP wants.
Unzipping a folder without it’s full hierarchy
If you’re just trying to unzip the contents of a directory within the zip file without retaining any of the hierarchy this folder is inside of, in the zip file then you’re out of luck. There is no method that provides this. The problem is with how zip files are created. They are typically built as a solid blob and so are not easily accessible in the way that most would think.
The only 2 approaches I’ve seen which can do this are to create a loop which goes through the index of a zip file, something like this:
Method #1
for i in $(unzip -l zipfile.zip); do # look for specific location # when there, make directory structure you want # unzip zipfile.zip $i -d /some/tmp/dir # move file from /some/tmp/dir to new directory done
The above can be done in a number of programming languages such as Perl, Python, etc.
Method #2
The other method involves making a copy of the zip file and then using the tool zipnote to manipulate the index of the files inside the archive, and then extract the files once the zip file’s index has been manipulated.
Unzipping a folder with it’s full hierarchy
If on the other hand you’re simply trying to extract a single folder from the zip file, you can use this command to do so:
$ unzip "/path/to/archive.zip" "in/archive/folder*" -d "/path/to/unzip/to"
Example
Sample directory structure.
$ tree . |-- 1 | |-- 2 | | |-- 3 | | | `-- 4 | | | `-- 5 | | | |-- file1 | | | |-- file2 | | | |-- file3 | | | |-- file4 | | | `-- file5 | | `-- afile | `-- afile `-- afile
$ zip -r ../afile.zip . adding: afile (stored 0%) adding: 1/ (stored 0%) adding: 1/afile (stored 0%) adding: 1/2/ (stored 0%) adding: 1/2/afile (stored 0%) adding: 1/2/3/ (stored 0%) adding: 1/2/3/4/ (stored 0%) adding: 1/2/3/4/5/ (stored 0%) adding: 1/2/3/4/5/file1 (stored 0%) adding: 1/2/3/4/5/file2 (stored 0%) adding: 1/2/3/4/5/file3 (stored 0%) adding: 1/2/3/4/5/file5 (stored 0%) adding: 1/2/3/4/5/file4 (stored 0%)
Confirming contents of zip file.
$ unzip -l ../afile.zip Archive: ../afile.zip Length Date Time Name --------- ---------- ----- ---- 0 11-07-2013 16:43 afile 0 11-07-2013 16:43 1/ 0 11-07-2013 16:43 1/afile 0 11-07-2013 16:43 1/2/ 0 11-07-2013 16:43 1/2/afile 0 11-07-2013 15:51 1/2/3/ 0 11-07-2013 15:51 1/2/3/4/ 0 11-07-2013 15:51 1/2/3/4/5/ 0 11-07-2013 15:51 1/2/3/4/5/file1 0 11-07-2013 15:51 1/2/3/4/5/file2 0 11-07-2013 15:51 1/2/3/4/5/file3 0 11-07-2013 15:51 1/2/3/4/5/file5 0 11-07-2013 15:51 1/2/3/4/5/file4 --------- ------- 0 13 files
Now we unzip just folder 5.
$ unzip afile.zip "1/2/3/4/5*" -d new/ Archive: afile.zip creating: new/1/2/3/4/5/ extracting: new/1/2/3/4/5/file1 extracting: new/1/2/3/4/5/file2 extracting: new/1/2/3/4/5/file3 extracting: new/1/2/3/4/5/file5 extracting: new/1/2/3/4/5/file4
$ tree . . `-- 1 `-- 2 `-- 3 `-- 4 `-- 5 |-- file1 |-- file2 |-- file3 |-- file4 `-- file5
I don’t think you can do it directly, although it can be done in a slightly roundabout way by unpacking the archive in a temporary location and moving the files into the current directory.
The problem is that you don’t really know about the structure of the zip file contents, so this is quite hard to solve in the general case. If what you want to do is to just «junk» the first directory component and dropping everything under it into the current directory, then we can write a shell script or shell function to do it. I’m going to show you an example for GNU bash; it shouldn’t be hard to adapt to other shells if you prefer something else. You can put this at the end of your ~/.bashrc to try it out.
unzip_to_current_dir() < zipfilenames="$@" for zipfilename in $; do tempdir=$(mktemp -d) unzip -d "$" "$" mv "$/*/*" ./ rm -rf "$" done >
The idea is to take the zip file names and then, one by one, create a temporary directory, unzip into that directory, move everything in the first level subdirectory of the unzip target directory into the current directory, and then clean up the temporary directory before moving on to the next file. This effectively removes the first component of the path in the zip file, so that unpacking a zip file that has a/abc, a/bcd, a/cde and a/def/ghi stores those files as ./abc, ./bcd, ./cde and ./def/ghi.
To easily test if this does what you want, take the four most-indented lines and execute them manually, one at a time, only substituting your own value for $ . I strongly suggest doing that in a directory that does not hold any critical data; I highly doubt that there is a data-corrupting bug in that script, but: if it breaks, you get to keep all the pieces.
Practical Examples of the Unzip Command in Linux
Got a zip file in the terminal? Learn how to use the unzip command in Linux with these practical examples.
If you have a zip compressed file, you can unzip it in the Linux command line. The unzip command in Linux is quite versatile and you can use it do a lot more than just extracting zip file.
I have discussed how to gzip a folder in Linux in the past. It’s time to see various usage of the unzip command. Before you do that, make sure that unzip has been installed on your system. You can use your distribution’s package manager to install the command.
On Ubuntu and Debian, you can use this command:
Once you have verified that, let’s see how to use unzip in Linux terminal.
Unzip command in Linux
The unzip command has a really simple syntax:
If you use it to extract a zip file without any option, it will extract all the files in the current directory:
unzip webdesign.zip Archive: webdesign.zip inflating: 339252-PAJF05-394.ai inflating: 339252-PAJF07-322.eps inflating: 339252-PALBTI-224.ai inflating: 339252-PALBTL-394.eps inflating: 339252-PALBTM-53.jpg inflating: License free.txt inflating: License premium.txt
And that’s not what you would want most of the time. It’ll just flood your current directory with all the extracted files.
ls 339252-PAJF05-394.ai 339252-PALBTI-224.ai 339252-PALBTM-53.jpg 'License premium.txt' 339252-PAJF07-322.eps 339252-PALBTL-394.eps 'License free.txt' webdesign.zip
1. Unzip to a directory
The expected behavior is that you should have the files extracted to a certain directory, normally with the same name as the zip file.
You can specify the target directory where you want to extract the files.
unzip -d target_directory zip_file
If the target directory doesn’t exist, it will be created. You cannot create nested directories in this manner though.
Do note that you can also put the target directory at the end but not all options can be added at the end.
unzip zip_file -d target_directory
2. See the content of the zip file without extracting
If you want to see what the zip file contains, you son’t always have to extract it first. You can use the -l option and it will show the content of the zip file.
As you can see, it also shows the timestamp of the files and the actual size of the individual files in bytes.
[email protected]:$ unzip -l webdesign.zip Archive: webdesign.zip Length Date Time Name --------- ---------- ----- ---- 205358 2018-06-18 23:14 339252-PAJF05-394.ai 996906 2018-06-18 23:14 339252-PAJF07-322.eps 213968 2018-06-20 00:00 339252-PALBTI-224.ai 1005362 2018-06-20 00:00 339252-PALBTL-394.eps 305531 2018-06-20 00:00 339252-PALBTM-53.jpg 1462 2018-06-20 09:45 License free.txt 1116 2018-06-20 09:45 License premium.txt --------- ------- 2729703 7 files
If you want, you can get more information like the compressed size, compression ratio by using the verbose mode with -v option. The CRC-32 in the output is the cyclic redundancy check.
[email protected]:$ unzip -v webdesign.zip Archive: webdesign.zip Length Method Size Cmpr Date Time CRC-32 Name -------- ------ ------- ---- ---------- ----- -------- ---- 205358 Defl:N 142097 31% 2018-06-18 23:14 792f2380 339252-PAJF05-394.ai 996906 Defl:N 221228 78% 2018-06-18 23:14 440390d3 339252-PAJF07-322.eps 213968 Defl:N 147568 31% 2018-06-20 00:00 cdb64dfc 339252-PALBTI-224.ai 1005362 Defl:N 226727 77% 2018-06-20 00:00 aed3d97a 339252-PALBTL-394.eps 305531 Defl:N 183363 40% 2018-06-20 00:00 e4ced90f 339252-PALBTM-53.jpg 1462 Defl:N 728 50% 2018-06-20 09:45 0eb9e17b License free.txt 1116 Defl:N 558 50% 2018-06-20 09:45 165dd84e License premium.txt -------- ------- --- ------- 2729703 922269 66% 7 files
3. Overwrite all the files without prompting
If there are already files with the same name in the directory where you are extracting the files, you’ll be promoted for each such files. You can force overwrite all the files with option -o .
unzip -o -d target_directory zip_file
4. Do not overwrite any files -n
If you don’t want any existing file to be overwritten by newly extracted files, use the -n option (stands for never overwrite).
unzip -n -d target_directory zip_file
5. Update files and create if necessary
This is slightly different the overwriting all the files. In this case, only those files will will be overwritten that have newer timestamp than the existing files. If a file doesn’t exist, it will be created.
You can achieve that with option -u:
unzip -u -d target_directory zip_file
6. Freshen existing files but create none
Slight change from the previous example here. In this one, it will update the existing files if they have older timestamp but it won’t create any new files even if they don’ exist.
The option -f allows you to do that:
unzip -f -d target_directory zip_file
7. Extract in quiet mode
When you unzip a file, it shows all the files that have been extracted on the display. Now imagine a zip file that has hundreds of files in it. If you extract it, your screen will be cluttered with the output.
You can use the quiet mode with option -q and you won’t see anything on the display:
unzip -q -d target_directory zip_file
8. Exclude files from extraction
You can also exclude certain files or certain type of files from being extracted.
unzip zip_file -x file_to_exclude
In my example, let’s say I don’t want to extract any .eps files.
[email protected]:$ unzip webdesign.zip -x *.eps Archive: webdesign.zip inflating: 339252-PAJF05-394.ai inflating: 339252-PALBTI-224.ai inflating: 339252-PALBTM-53.jpg inflating: License free.txt inflating: License premium.txt
Those were some of the most common examples of the unzip command in Linux. You can always check its man page to learn about more options.
Do you use some other option with unzip frequently? Why not share it with us in the comments?
How to unzip into a given directory
I have a list with the path of .zip files, gathered by the command below. I want to unzip them into that directory, where they exist.
find . -name "*.zip" -print > outfile.txt
run_all outfile.txt 'unzip -u $1'
1 Answer 1
unzip -d output_dir/ zipfiles.zip
#!/bin/bash for i in `cat outfile.txt`; do output_dir=$(dirname $i) unzip -d $output_dir $i done
EDIT: As @dessert suggests, you may do this as a better alternative:
while IFS='' read -r i || [[ -n "$i" ]]; do unzip -d $ $i; done
This will break on a single whitespace in a path, you should rather use read . Additionally the bash shell can do dirname 's work with parameter expansion: while IFS='' read -r i || [[ -n "$i" ]]; do unzip -d $ $i; done
You must log in to answer this question.
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533
Ubuntu and the circle of friends logo are trade marks of Canonical Limited and are used under licence.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.