Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command to zip a directory using a specific directory as the root

I'm writing a PHP script that downloads a series of generated files (using wget) into a directory, and then zips then up, using the zip command.

The downloads work perfectly, and the zipping mostly works. I run the command:

zip -r /var/www/oraviewer/rgn_download/download/fcst_20100318_0319.zip /var/www/oraviewer/rgn_download/download/fcst_20100318_0319 

which yields a zip file with all the downloaded files, but it contains the full /var/www/oraviewer/rgn_download/download/ directories, before reaching the fcst_20100318_0319/ directory.

I'm probably just missing a flag, or something small, from the zip command, but how do I get it to use fcst_20100318_0319/ as the root directory?

like image 500
Tarka Avatar asked Mar 18 '10 20:03

Tarka


People also ask

How do I zip the contents of a directory?

Press and hold (or right-click) the file or folder, select (or point to) Send to, and then select Compressed (zipped) folder. A new zipped folder with the same name is created in the same location.

How do I zip a directory in Linux?

The easiest way to zip a folder on Linux is to use the “zip” command with the “-r” option and specify the file of your archive as well as the folders to be added to your zip file. You can also specify multiple folders if you want to have multiple directories compressed in your zip file.

How do I zip a folder in command prompt?

If you open a terminal console in the parent directory, or used the cd command to navigate there from the command line, you should then be able to run the command on the folder. The syntax is ' zip -r <zip file name> <directory name> '. The '-r' option tells zip to include files/folders in sub-directories.


2 Answers

I don't think zip has a flag to do that. I think the only way is something like:

cd /var/www/oraviewer/rgn_download/download/ && \    zip -r fcst_20100318_0319.zip fcst_20100318_0319 

(The backslash is just for clarity, you can remove it and put everything on one line.)

Since PHP is executing the command in a subshell, it won't change your current directory.

like image 119
JayM Avatar answered Oct 14 '22 15:10

JayM


I have also get it worked by using this command

exec('cd '.$_SERVER['DOCUMENT_ROOT'].' && zip -r com.zip "./"');

like image 25
vivek barsaiyan Avatar answered Oct 14 '22 17:10

vivek barsaiyan