Unzip archive to an existing directory structure - linux

I'm looking at bring the content of one WP blog over to another as I will be using WPML to server regional content instead of multiple sites. So not strictly a WP question, more command line.
This may seem an obvious or stupid question, but if I bring over the other 'uploads' folder as a zip and unzip to the wp-content folder, will the contents merge into the existing folders or overwrite what is already there.
If it's the latter, is there a switch I can append to ensure files are merged?
Thanks in advance,
Tom

When unzip finds a file that already exists in the destination, it will ask you if you want to overwrite it. You can then type y to overwrite it, A to overwrite all files, N if you don't want to overwrite any of them etc.
Example:
$ unzip archive.zip
Archive: archive.zip
replace foo? [y]es, [n]o, [A]ll, [N]one, [r]ename:

Try using
unzip -o <filepath/zipfile.zip> -d <path where you want files>
Best use: unzip --help

Related

How can we specify the unzip or 7za command in linux to extract multiple zip files into one folder while keeping all duplicates?

I currently have about 10 zip files I would like to extract into one folder. Each zip file contains around 1000 images. As a result, lots of the names of the images are duplicated. For example, in the first zip file, we have things like Img.jpg, Img(1).jpg, Img(2).jpg. I know that to extract multiple zip files into a single folder, I would do something like:
unzip '*.zip'
However, when it tries to put a file from the first zip file that has the same name as a file in the second zip file, it starts to ask:
replace duplicatefile.mp4? [y]es, [n]o, [A]ll, [N]one, [r]ename:
At this point, what do I do if I want to keep ALL files, including the duplicates, and possibly have them named to image(1).jpg instead?
In short, is there a way to call the unzip command on all the zip files, have them extracted into a single folder, without losing any files due to same names?
Thanks.
Invoke unzip --help for details.
But it appears unzip *.zip -n should do the trick?
(Make sure it does what you expect before going ahead!)

How to use command zip in linux that folder have short path?

I used command zip in linux (RedHat), this is my command:
zip -r /home/username/folder/compress/zip.zip /home/username/folder/compressed/*
Then, i open file zip.zip, i see architecture as path folder compress.
I want to in folder zip only consist list file *.txt
Because i used this command in script crontab hence i can't use command cd to path folder before run command zip
Please help me
I skimmed the zip man page and this is what I have found. There is not an option archive files relative to a different directory. The closest I have found is zip -j which removes the entire path and stores the files directly in the zip rather than sub directories. I do not know what happens in the case of file name conflicts such as if /home/username/folder/compressed/a.txt and /home/username/folder/compressed/subdir/a.txt both exist. If this is not a problem for you, you can use this option, but I am concerned because you did specify the -r option indicating that you expect zip to traverse sub folders.
I also thought of the possibility that your script could somehow call zip with a different working directory, but I took a look at this unix stack exchange page and it looks like their options use cd.
I have to admit I do not understand why you cannot use cd and I am very curious about it. You said something about using crontab, but I have never heard of anything wrong with changing directories in a crontab script.
I used option -j in command zip
zip -jr /home/username/folder/compress/zip.zip /home/username/folder/compressed/*
and i was yet settled this problem, thanks

Sync zip file with folder?

I know specifying a zip file in the command add the files to the zip file itself, but how can I really sync a folder having already a zip file so that it does add new files, replace modified files and delete absent files in the folder?
The command-line "zip" tool has the -FS (or --filesync) flag that does exactly this (updates existing, adds new, deletes removed).
See http://www.info-zip.org/mans/zip.html.
The command-line "zip" command has options for --freshen (update existing files only), --update (update and add files), and --delete to remove, but I couldn't find a way to combine them all into one command.
Since the zip file probably needs to be completely rewritten during processing anyways, why not just delete the old zip file and create a new one from scratch?

How to create a zip file without entire directory structure

I am trying to create a zip file and want to preserve most of the directory structure, but not the rootdir as defined from the command line. The command I'm using is:
zip -r out.zip /foo/bar/
I'd like it to recurse through bar and add all files with preserved directory structure (which it does). However I do not want 'foo' to be the top level directory in the zip file created. I would like bar to be the top level directory.
Is there any easy way to go about this? I realize I could change directories before zipping to avoid the problem, but I'm looking for a solution that doesn't require this.
This should do it:
cd /foo/bar/
zip -r ../out.zip *
The archive will be in /foo/out.zip
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.
7z a -tzip out.zip -w foo/bar/.
If someone stumbles upon this and is not satisfied with the above solution, here follows a very simple workaround to not zip long subdirectories. It involves temporarily creating a folder in C:/, and after zipping simply deleting it:
ZipFiles <- list.files(".../ZipFiles") # Insert your long subdirectory into .../
dir.create("C:/ZipFiles")
dir.create(".../FolderToBeZipped")
file.copy(from = ZipFiles,to = "C:/ZipFiles")
zip(".../FolderToBeZipped",
files = "C:/ZipFiles")
unlink("C:/ZipFiles",recursive = TRUE)
The result then is .../FolderToBeZipped.zip/ZipFiles/
The benefit is that you need not be within the subdirectory (or project) when executing the code.

Can you use tar to apply a patch to an existing web application?

Patches are frequently released for my CMS system. I want to be able to extract the tar file containing the patched files for the latest version directly over the full version on my development system. When I extract a tar file it puts it into a folder with the name of the tar file. That leaves me to manually copy each file over to the main directory. Is there a way to force the tar to extract the files into the current directory and overwrite any files that have the same filenames? Any directories that already exist should not be overwritten, but merged...
Is this possible? If so, what is the command?
Check out the --strip-components (or --strippath) argument to tar, might be what you're looking for.
EDIT: you might want to throw --keep-newer into the mix, so any locally modified files aren't overwritten. And I would suggest testing new releases on a development server, then using rsync or subversion to carry over the changes.
I tried getting --strip-components to work and, while I didn't try that hard, I didn't get it working. It kept flattening the directory structure. In searching, I came across the following command that seems to do exactly what I want:
pax -r -f patch.tar -s'/patch///'
It's not tar, but hey, it works... Replace the words "patch" with whatever your tar file name is.
The option '--strip-components' allows you to trim parts of the embedded filenames. With that it is possible to do what you want.
For more help check out http://www.gnu.org/software/tar/manual/html_section/transform.html
I have just done:
tar -xzf patch.tar.gz
And it overwrites all the files that the patch contains.
I.e., if the patch was created for the contents of the app folder, I would extract it there. Results would be like this:
tar.gz contains: oldfolder/someoldfile.txt, oldfolder/newfolder/newfile.txt
before app looks like:
app/oldfolder/someoldfile.txt
Afterwards, app looks like
app/oldfolder/someoldfile.txt
oldfolder/newfolder/newfile.txt
And the "someoldfile.txt" is actually updated to what was in the tar.gz
Maybe this doesn't work with regular tar, only tar.gz. But I doubt it. I think it should work for everything, as long as user has write permissions.

Resources