2

Using terminal command "unzip" seems to be extremely useful when deploying a CMS to a remote server, since you can transfer a zip and unzip it remotely. However, I cannot find a way to unzip the all files directly to the root of the directory the .zip-file resides in. By default it creates a folder with the same name as the .zip-file and extracts the files to this folder, after which I need to do a mv command to move the contents of the folder to the root.

Can I somehow do this in one step using unzip?

3 Answers 3

0

The specific answer to your question is no. The unzip that ships with OS X has no way to change directories in the zip on the fly. One thing to note however, is that zip is not 'creating the directory'. It is part of the archive. A properly created zip archive will not have this issue if you are controlling the creation process:

$ ls -l
total 0
-rw-r--r--  1 bsmith  staff  0 Sep 30 22:01 b
-rw-r--r--  1 bsmith  staff  0 Sep 30 22:01 c
$ zip -r a.zip .
  adding: b (stored 0%)
  adding: c (stored 0%)
$ cd ../b
$ unzip ../a/a.zip
Archive:  ../a/a.zip
 extracting: b                       
 extracting: c                       
$ ls -l
total 0
-rw-r--r--@ 1 bsmith  staff  0 Sep 30 22:01 b
-rw-r--r--@ 1 bsmith  staff  0 Sep 30 22:01 c
1
  • 1
    You seem to be right. I will keep using the mv command after the unzip command.
    – zkwsk
    Oct 2, 2011 at 14:52
3

Try using the -d parameter to specify the root directory to extract to. You can use '.' or '~' as your directory and avoid a situation where everything is placed in a default directory carrying the zip file name. Here's the documentation:

[-d exdir] An optional directory to which to extract files.
By default, all files and subdirectories are recreated in the current directory; the -d option allows extraction in an arbitrary directory (always assuming one has permission to write to the directory). This option need not appear at the end of the command line; it is also accepted before the zipfile specification (with the normal options), imme- diately after the zipfile specification, or between the file(s) and the -x option. The option and directory may be concatenated without any white space between them, but note that this may cause normal shell behavior to be sup- pressed. In particular, -d ~'' (tilde) is expanded by Unix C shells into the name of the user's home directory, but-d~'' is treated as a literal subdirectory ``~'' of the current directory.

1
  • 2
    I know the -d option, which enables me to extract to a different directory than the source of the zip-file, a new file with the name of the zip-file will still be created.
    – zkwsk
    Sep 30, 2011 at 17:58
0

You could use the -j flag for the same.

Example:

$ mkdir Testing && cd Testing
~/Testing $ touch one two three four five
~/Testing $ cd ..
$ zip -r Testing Testing
$ ls -la | grep Testing


drwxr-xr-x   7 username  staff    238 Aug  2 20:03 Testing
-rw-r--r--   1 username  staff    924 Aug  2 20:03 Testing.zip

Now,

$ rm -rf Testing
$ unzip -j Testing.zip
$ ls -la

-rw-r--r--   1 username  staff     0 Aug  2 20:03 five
-rw-r--r--   1 username  staff     0 Aug  2 20:03 four
-rw-r--r--   1 username  staff     0 Aug  2 20:03 one
-rw-r--r--   1 username  staff     0 Aug  2 20:03 three
-rw-r--r--   1 username  staff     0 Aug  2 20:03 two

This will help you extract the contents of the archive and not the contained folder.

$ man unzip

-j     junk paths.  The archive's directory structure is not recreated;
       all files are deposited in the extraction directory 
       (by default, the current one).

For more information, refer to man pages for unzip.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .