I want to zip a sub directory and send it elsewhere, the problem I have is when I use the zip command it also includes all the unwanted layers of directories I do not want.
e.g.
zip -r /new.zip /Unwanted1/Unwanted2/wanted1/wanted2/file.txt
###unzipped produces the following
/Unwanted1/Unwanted2/wanted1/wanted2/file.txt
###I want
/wanted1/wanted2/file.txt
update: The directory I want to zip has lots of nested directories, hence the -r. Also it would be great to deal with absolute paths, the old solution used to cd about the place but I just didn't really like it. Perhaps that is the best (only) way.
Syntax : $zip –m filename.zip file.txt 4. -r Option: To zip a directory recursively, use the -r option with the zip command and it will recursively zips the files in a directory. This option helps you to zip all the files present in the specified directory.
Creating a zip folder allows files to be organized and compressed to a smaller file size for distribution or saving space. Zip folder can have subfolders within this main folder.
To zip (compress) a file or folder Locate the file or folder that you want to zip. 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.
Select the “Files” option : your file explorer should start automatically. Now that you are in your file explorer, select multiple folders by holding the “Control” key and left-clicking on all the folders to be zipped. When you are done, right-click and select the “Compress” option.
You have to change directories to wherever you want to start, or maybe there's an option to 'zip' to do that for you. If you're too lazy to read through the gazillion options in the man page, like I am, just 'cd' first, even in a script. Or you can enclose the whole thing in parenthesis without affecting your shell or script's working directory:
(cd Unwanted1/Unwanted2; zip -r ~/new.zip ./wanted/file.txt)
Or since you're using -r you presumably want more than just file.txt, so:
(cd Unwanted1/Unwanted2; zip -r ~/new.zip wanted)
Or use pushd/popd to jump in/out:
pushd Unwanted1/Unwanted2
zip -r ~/new.zip wanted
popd
This works in sh/bash/csh/tcsh.
Simplest, idiomatic way:
(cd ./Unwanted1/Unwanted2 && zip -r ../../new.zip ./wanted/file.txt)
Now zip, tar, rar, cpio and friends all have options to manage the same, but they may differ from version to version and certainly from archive utility to archive utility. I suggest you learn to work with the shell to do the work, and you can use any archive format you desire
Another way:
(cd ./Unwanted1/Unwanted2 && find . -name "*.txt" -print) | zip new.zip -@
Well one simple way around might be to do
cd ./Unwanted1/Unwanted2
zip -r ../../new.zip ./wanted/file.txt
But I don't know what your situation is. Perhaps you're required to run from the base directory? Also the "-r" option is used when you want to recursively traverse directories - the example you provided is using one file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With