Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 0 down vote accepted

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
share|improve this answer
    
You seem to be right. I will keep using the mv command after the unzip command. –  funkylaundry Oct 2 '11 at 14:52

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.

share|improve this answer
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. –  funkylaundry Sep 30 '11 at 17:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.