I need to create a zip file using this command:
zip /dir/to/file/newZip /data/to/zip/data.txt
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.
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).
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 -;
Or in-line version
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
Use the -j option:
-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 :(
Here is a good solution on how to get it done:
https://superuser.com/questions/119649/avoid-unwanted-path-in-zip-file
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.
Just use the -jrm option to remove the file and directory
structures
zip -jrm /path/to/file.zip /path/to/file
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
Related
Suppose Directory1 has
1.File1
2.File2
3.Subdirectory1
Subdirectory1 has :
3.1. File 3
3.2. File 4
and
Directory2 has
1.File1
2.File3
3.Subdirectory1
Subdirectory1 has :
3.1. File 3
3.2. File 6
If any file is uniquely present in Directory2 it has to be deleted.
If the file is present in both Directory1 and Directory2 , files in Directory1 has to be copied to Directory2 with the same folder structure [Updates].
Simply use diff, eg :
diff -r dir1 dir2 | grep dir1
Only in dir1: file2
Only in dir1/subdir1: file4
Only in dir2/subdir1: file6
You can then awk, or store the result in a temporary file and use it in your script.
You seem to talk about a mirroring function, see the nice Open Source tool rsync.
https://rsync.samba.org/
It can do all of that and even more (also remote synchronization via LAN or via SSH if needed).
rsync -options --otherOptions sourceDir targetDir
Usually you would use these commandline options:
rsync -av /src/foo /dest
or
rsync -av /src/foo/ /dest/foo
Note: if you omit the trailing "/" of /src/foo, then rsync will mirror to /dest and create a foo subdir. You have either the one or the other choice how to use this command.
I have zip file with 1 directory that stored another directory and the second hold some files. How can I unzip in one command in order to get dir 2 along with it's files.
thanks,
Did you try typing man unzip?
unzip zipfile.zip dir1/dir2
If you want to ignore the zip's directory structure, use the -j (Junk paths) option:
unzip -j zipfile.zip dir1/dir2 -d dir2
This will store all files recursively found under dir1/dir2 into dir2 under the current folder.
I have a directory tree like this :
dir1/
file11
file12
file13
...
file1548216479524594
dir2/
file21
file22
dir3/
dir31/
file311
file312
dir32/
file321
I would like to rsync entire directory tree but without content of directory dir1.
If I use the basic rsync command :
rsync --progress -v -ar --delete --exclude="dir1/*" src/ dst/<br>
It works. But if I use -n to make a dry run before execute, it lasts very long because dir1 contains a lot file (I do not know why during the dry-run it lists all files, even those excluded).
If I use --exclude="dir1/", the dry-run is fast but I don't have my directory tree.
How can I do a rsync dry run fast (avoiding recursively dir1 files which are very numerous.) with my entire directory tree excluding all content of dir1 ?
In recent versions of rsync, you can use the -F option and put a file ".rsync-filter" in the directory src, containing:
- dir1/***
That seemed to work for me. I'm assuming that your hierarchy above is all under "src/".
In Linux I can zip all(except hidden files) in current directory by doing:
zip 1.zip *
But how do I include the hidden files?
EDIT: The correct way is zip -r 1.zip .
The commands shown in my previous answer below are incorrect because they also include the parent directory.
Have you tried this:
zip yourfile.zip sourcedir/* .*
or you in your case
zip 1.zip * .[^.]*
It should include all hidden files also.
Or you can add more simple
zip 1.zip ./
Just to be sure it is not forgotten since this is a forum for developers and a good number of us use git.
An easy way to get only what you want in the zip is to use git archive -o filename.zip branch
If you want to zip all files (+hidden files)
Kindly using: zip -r namefiles.zip .
The "." is all files in folder.
zip -r namefiles.zip "folder will zip"
On macOS 10.15.7 I had to separatelly add all dot leading files (\.*) and rest of the files (*):
zip -r file.zip \.* *
if you don't have rights to save zip file in current dir you can go to dir where you have rights and type
zip -r 1.zip /path/to/source/dir/.
However when if in .../some_dir you type
unzip 1.zip
then your files will be decompress into .../some_dir/path/to/source/dir/
zip -r 1.zip .* -x "../*"
Just doing zip -r 1.zip .* will include the parent folder as well so the trick is to exclude the parent folder using -x "../*"
If you'd like to save some subdirectory of the current directory recursively with hidden and regular files just type
$ zip -r backup_subdirectory.zip backup_subdirectory/. backup-subdirectory/*
And for unzipping:
$ unzip backup_subdirectory.zip
Or even simpler by using tar for creating an archive:
$ tar czvf backup_subdirectory.tar.gz backup_subdirectory/
And for extracting all files from the archive:
$ tar xzvf backup_subdirectory.tar.gz
I have a directory structure and files like this
data/
data/a.txt
data/folder/
data/folder/b.txt
data/folder/folder/
data/folder/folder/c.txt
...
a.txt, b.txt, and c.txt are large files that are computer-generated and renewed frequently. They should NOT be backuped -- but I want to backup the directory structure :
data/
data/folder/
data/folder/folder/
How can I do this with rsync and --exclude-from, without specifying every folder, but something like rsync -a data/* --exclude-from=exclude.rsync "" --onlyfoldersandnotfiles""?
Thanks for help !
rsync -a --include='*/' --exclude='*' source/ destination/
Basically, first include all directories, then exclude all files.
$ rsync -a -f"+ */" -f"- *" source/ destination/
"The two -f arguments mean, respectively, "copy all directories" and then "do not copy anything else"."
Further details: http://psung.blogspot.com/2008/05/copying-directory-trees-with-rsync.html
rsync -a -f"-! */" source/ destination/
Filter rule means "Exclude anything that's not a directory."
If you want to sync everything except one folder but you still want to keep the directory structure of that excluded folder, you should do the following:
$ rsync -a -f"+ /var/log/**/*/" -f"- /var/log/**/*" source/ destination/
See the exclude-list and the rsync command as an example.