Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unzip and move a downloaded file - Linux [closed]

Surprisingly I could not find a straight-forward answer to this question on here yet. I am still learning Linux. Say I have downloaded a zip file to my Downloads folder. Now, I want to move it into a protected folder, like /opts or /var. Is there a good command to both sudo move AND unzip the file to where I need it to go?

like image 367
Houdini Avatar asked Jan 17 '13 14:01

Houdini


2 Answers

If you wish to perform two separate operations (move and extract) then you have no option but to use two commands.

However, if your end goal is to extract the zip file to a specific directory, you can leave the zip file where it is and specify an extraction directory using the -d option:

sudo unzip thefile.zip -d /opt/target_dir

From the manpage:

[-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), immediately 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 suppressed. 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.

like image 67
Shawn Chin Avatar answered Sep 20 '22 12:09

Shawn Chin


sudo mv <file_name> /opts && unzip /opts/<file_name>

Also you may specify the unzip destination to unzip so you can do this in a single command. This however will be a bit different from the command above as the zip will be kept in its current location, only the unzipped files will be extracted to the pointed destination.

like image 23
Ivaylo Strandjev Avatar answered Sep 18 '22 12:09

Ivaylo Strandjev