Linux zip no path

Zipping files with relative path?

I’m trying to create backups of my USB key. For that, I’d need to zip the content of my USB key ( /Volumes// ). I have this code for the moment
zip -r /Volumes//* That seems to work, except the fact that when I extract my archive, I get : (For simplfication purpose (and laziness), +().zip is Archive.zip )

I know it’s something about absolute/relative paths but that’s all I know. How could I do this ? PS : I’m on MacOS

2 Answers 2

It stores just the name of the saved files (junk the path), and does not store directory names. By default, zip will store the full path (relative to the current path).

I tried but it gives me an error, saying that I have to files with the same name, and that it is propably because I have the -j option, so what can I do?

Try changing your working directory into the USB drive prior to zipping your files. Include the original -r option but not the -j option. So: cd /Volumes// then zip -r *

-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).

However, -j will discard all path information, which caused all files be put in same package without any path information.

Method 1: Change current directory( cd )

cd /Volumes; zip -rq $(OLDPWD)/Archive.zip ; cd - 

Method 2: Symlink will help

ln -s /Volumes/ . # create symlink to avoid cd zip -rq Archive.zip rm # remove symlink 

Источник

Zip an archive without including parent directory

When I try to zip it, it ends creating a zip archive with the v folder instead of the contents of it (the sub directories and files) How can I avoid this?

7 Answers 7

(cd directory && zip -r ../out.zip .) 

It keeps the main shell in the same directory and only changing the directory of the sub-shell which dies after the command.

I wanted to add this as a comment to @Alfred UC’s answer but I don’t have enough reputation for that.

Читайте также:  Самсунг scx 4200 драйвер линукс

Use the -j or —junk-paths option in your zip command.

-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). 

This will omit all folders in the archive, and if you have duplicate file names in different folders, this will blow up. This has its place, but only if you don’t care about folders and just want files. Not what the OP was asking for, but still good knowledge

So if I understand correctly, you are trying to archive the files & folders in a particular folder but without including the root folder.

where test.txt and test2.txt would be stored in the zip, but not /test/

You could cd into the /test/ directory then run something like,

Which would create an archive in the same folder named filename.zip. Or if running it from outside the folder you could run,

The /* is the part that includes only the contents of the folder, instead of the entire folder.

Edit: OP wanted multiple zips, solution ended up being a bit of a hack, I am curious as to whether there is a better way of doing this.

for d in */ ; do base=$(basename "$d") ; cd $base ; zip -r $base * ; mv "$.zip" .. ; cd .. ; done; 

Ok I ran the script and it created a the parent directory in the archive i want only the contents of the folder to be in the archive

my mistake, missed a bit of it. added a -j parameter which i just looked up, apparently skips the part you don’t want. try this: for d in */ ; do base=$(basename «$d») ; zip -rj «$.zip» «$d» ; done;

as @smihael stated, zip -r test.zip test/* does not work. is there any correct way to achieve this from outside the directory?

$ cd somedir ; zip -r ../zipped.zip . * ; cd .. 

While this is a correct answer, the introduction doesn’t sound like you’re convinced yourself. It would also help to explain a little bit, what the command does.

This was my solution too, but I dislike it because it involves cd . Was hoping there was a way to specify the stored path more precisely.

cd `dirname path/to/archive` && zip -rq $OLDPWD/arhive.zip . && cd - 

This works not only with flatten tree (like -j ) and you can specify any dir (not only children)

(cd MyDirectory && zip -r - .) >MyArchive.zip 

This lets you specify the resulting filename relative to the current directory, rather than relative to the target directory.

Читайте также:  Gnu linux source code

As a bonus, this overwrites the archive instead of adding new files to it, which was desired in my case.

Instead of . you can specify files/directories relative to the target directory that you want to include.

Unlike -j it preserves paths relative to the target directory, rather than flattening all files into a single directory.

Источник

Create zip file and ignore directory structure

This works, but the created zip file creates a directory structure mimicking the directory to the raw file. It is a lot of extra folders that I don’t need. I didn’t find an answer in a cursory glance over the man page or a Google hunt.

I was thinking this question predated all of those other stack exchanges but alas superuser’s post actually is older than this one. If there is something I can do here to help let me know. This must be quite the hot answer since google still drives so much traffic here. 😀

7 Answers 7

-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). 

Using -j won’t work along with the -r option.
So the work-around for it can be this:

cd path/to/parent/dir/; zip -r complete/path/to/name.zip ./* ; cd -; 
cd path/to/parent/dir/ && zip -r complete/path/to/name.zip ./* && cd - 

you can direct the output to /dev/null if you don’t want the cd — output to appear on screen

Actually, you might want do the following: cd path/to/parent/dir/ && zip -r ../../../../name.zip ./* && cd —

@eddyP23 How does it make any difference ? Rather it will cause you to add no. of parentDir(..) for each directory in the path 😐

The result is the same, I agree. But in automated scripts that run in many different environments, you usually avoid global paths, because you have no idea what the global path will be. But from the cd path/to/parent/dir/ you can calculate number of double dots ../ easily.

 -j 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 path). 

Somewhat related — I was looking for a solution to do the same for directories. Unfortunately the -j option does not work for this 🙁

Читайте также:  Команда вывод устройств linux

Alternatively, you could create a temporary symbolic link to your file:

ln -s /data/to/zip/data.txt data.txt zip /dir/to/file/newZip !$ rm !$ 

This works also for a directory.

Retain the parent directory so unzip doesn’t spew files everywhere

When zipping directories, keeping the parent directory in the archive will help to avoid littering your current directory when you later unzip the archive file

So to avoid retaining all paths, and since you can’t use -j and -r together ( you’ll get an error ), you can do this instead:

cd path/to/parent/dir/; zip -r ../my.zip "../$(basename "$PWD")" cd -; 

The «../$(basename «$PWD»)» is the magic that retains the parent directory.

So now unzip my.zip will give a folder containing all your files:

parent-directory ├── file1 ├── file2 ├── dir1 │ ├── file3 │ ├── file4 

Instead of littering the current directory with the unzipped files:

file1 file2 dir1 ├── file3 ├── file4 

However in the ZIP archive an additional unwanted parent directory /../ is created, like /../parent/sub/

Just use the -jrm option to remove the file and directory structures

zip -jrm /path/to/file.zip /path/to/file 

be carefull, -m —move Move the specified files into the zip archive; actually, this deletes the target directories/files after making the specified zip archive. If a directory becomes empty after removal of the files, the directory is also removed. No deletions are done until zip has created the archive without error. This is useful for conserving disk space, but is potentially dangerous so it is recommended to use it in combination with -T to test the archive before removing all input files.

The «-m» flag being on this could HUGELY impact people, possibly even give a couple scares, or even worse, loss of data (without, for example, the -T flag as the commenter above mentioned). -1

Источник

Оцените статью
Adblock
detector