Command to zip a directory using a specific directory as the root
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.
Using bash zip from within target directory vs from outside
Yes, these commands act differently. When you use zip
it will by keep the paths relative to how you ran the command, so running zip from different working directories will affect the result.
Example:
.
└── test
├── file1
├── file2
├── file3
└── foo
├── file4
└── file5
If I am in the root directory in this example and use the following command:
zip -r test.zip test/*
I get the following result:
$ unzip -l test.zip
Archive: test.zip
Length Date Time Name
--------- ---------- ----- ----
0 2019-11-08 16:51 test/file1
0 2019-11-08 16:51 test/file2
0 2019-11-08 16:51 test/file3
0 2019-11-08 16:53 test/foo/
0 2019-11-08 16:52 test/foo/file4
0 2019-11-08 16:53 test/foo/file5
--------- -------
0 6 files
However if I change directory into test
, and this command:
cd test
zip -r ../test2.zip *
I get the following zip file:
$ unzip -l test2.zip
Archive: test.zip
Length Date Time Name
--------- ---------- ----- ----
0 2019-11-08 16:51 file1
0 2019-11-08 16:51 file2
0 2019-11-08 16:51 file3
0 2019-11-08 16:53 foo/
0 2019-11-08 16:52 foo/file4
0 2019-11-08 16:53 foo/file5
--------- -------
0 6 files
Aside:
As an aside, if you do not have a directory structure, you can specify -j
which will remove all paths and store the files all in the root of the zip file:
# from the root of the example structure
zip -j test3.zip test/*
Produces:
$ unzip -l test3.zip
Archive: test.zip
Length Date Time Name
--------- ---------- ----- ----
0 2019-11-08 16:51 file1
0 2019-11-08 16:51 file2
0 2019-11-08 16:51 file3
0 2019-11-08 16:52 file4
0 2019-11-08 16:53 file5
--------- -------
0 5 files
Create zip file and ignore directory structure
You can use -j
.
-j
--junk-paths
Store just the name of a saved file (junk the path), and do not
store directory names. By default, zip will store the full path
(relative to the current directory).
linux how to add a file to a specific folder within a zip file
If you need to add the file to the same folder as in the original directory hierarchy, then you just need to add the full path to it:
zip -g xxx.zip folder/file
Otherwise, probably the easiest way to do that is to create the same layout you need in the zip file in a temporary directory.
How to add files and dirs to a zip file without the root dir?
I understand that you do not want to cd
, but...
If all else fails and you are hoping to keep your call to zip down to one line, you could chain the calls and use a relative path to output zip in the root:
For Windows:
cd base & zip -r ..\out.zip * & cd ..
For Linux/Unix:
cd base ; zip -r ../out.zip * ; cd ..
How to create a zip archive of a directory?
As others have pointed out, you should use zipfile. The documentation tells you what functions are available, but doesn't really explain how you can use them to zip an entire directory. I think it's easiest to explain with some example code:
import os
import zipfile
def zipdir(path, ziph):
# ziph is zipfile handle
for root, dirs, files in os.walk(path):
for file in files:
ziph.write(os.path.join(root, file),
os.path.relpath(os.path.join(root, file),
os.path.join(path, '..')))
with zipfile.ZipFile('Python.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
zipdir('tmp/', zipf)
How to create a zip file without entire directory structure
I don't believe zip has a way to exclude the top level directory. I think your best bet would be to do something like:
pushd /foo; zip -r out.zip ./bar; popd;
But this is exactly the sort of answer you said you didn't want.
Related Topics
What Is File Hole and How Can It Be Used
How to Catch Duplicate Entries in Text File in Linux
Ansible Conditional Based on Stdout of Result
Unable to Start Rstudio in Centos Getting Error "Unable to Connect to Service"
How to Force a Firefox Page Refresh from Linux Console
Installing Qt on Linux, Cannot Find -Lgl
How to Keep Executable Code in Memory Even Under Memory Pressure? in Linux
Dhcp Server to Redirect Any Url to Landing Page
I Want to Use "Awk" or Sed to Print All the Lines That Start with "Comm=" in a File
How to Trace Per-File Io Operations in Linux
Extract List of File Names in a Zip Archive When 'Unzip -L'
Shell Script Working Fine Without Shebang Line? Why
How to Open a File in Assembler and Modify It
What Does "< /Dev/Null >& /Dev/Null" at the End of a Command Do
How to Copy File to Stopped Docker Container