Unzip and move the downloaded file - Linux

Surprisingly, I could not find a direct answer to this question. I am still learning Linux. Let's say I uploaded a zip file to the My Downloads folder. Now I want to move it to a protected folder, for example / opts or / var. Is there a good command for sudo move AND unzip file where I need it?

+4
source share
3 answers

If you want to perform two separate operations (move and retrieve), then you have no option but to use two commands.

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

 sudo unzip thefile.zip -d /opt/target_dir 

From manpage :

[- d exdir]

Additional directory for extracting files. By default, all files and subdirectories are recreated in the current directory; the -d option allows extraction in an arbitrary directory (always assuming that anyone has permission to write to the directory). This parameter should not appear at the end of the command line; it is also accepted before the zipfile specification (with the usual parameters), immediately after the zipfile specification, or between the file (s) and the -x option. The option and directory can be combined without any space between them, but note that this can lead to suppression of the normal behavior of the shell. In particular, `` -d ~ '' (tilde) is expanded by Unix C shells in the name of the user's home directory, but `` -d ~ '' is considered as the literal subdirectory `` ~ '' of the current directory.

+13
source

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

In addition, you can specify the post office for unpacking, so that you can do this in one command. However, this will be slightly different from the above command, since the zip will be stored in the current location, and only unzipped files will be extracted to the specified destination.

+1
source
 unzip -d [target directory] [filename].zip 
+1
source

All Articles