I am on Mac OS.
I have a directory with round about 3000 files and several subdirectories (wordpress installation)
Now I have to find all the files in a similar directory (have to separate master and child installation) that are additional files and have to copy them away into another directory.
I use this command:
$ diff -rq dt-the7 dt-the7-master-from-Yana|grep 'Only in dt-the7'|awk {' print $3 $4 '}|sed 's/:/\//g'|xargs -J {} rsync -av {} neu/
but somehow a certain file 3d.png and a list of other that should be in a subdir of the destination dir are copied into the root dir of the destination.
Any idea why that might be?
It makes no difference whether I use cp, rsync or ditto
You need the -R relative option on your rsync command.
Without this rsync just copies the item referenced rather than the path referenced, so items at the root level are copied as you expected but items in sub-directories are also copied to the root, which is not what you wanted.
With the option rsync takes account of the relative path and recreates it at the destination.
An example with another command might help, consider:
cp A/B.txt C/
that will copy B.txt into C, it does not create a folder A in C which in turn contains the file B.txt. rsync without -R behaves like that cp command, with -R it creates the A directory in C.
HTH
Related
I need to sync a local directory (destination) from the remote server directory (source) with the --delete option.
I've got this:
Local directory is /home/user/config and into it
removeit.txt
bar.csv
foo.h
config.conf
scripts <- Is a directory
logs <- Is adirectory
The remote directory is config and into it
bar.csv
foo.h
I want in the local directory after rsync
bar.csv
foo.h
config.conf
scripts <- Is a directory
logs <- Is adirectory
I've tested with multiple options but I can't get a entire rsync command with my needs.
rsync -avz --min-size=1 --delete -e "ssh" user#example.com:./config/ ./config --exclude ./config/scripts --exclude ./config/logs --exclude ./config/device.conf --dry-run
With a list of include files
rsync -avz --min-size=1 --include-from=list --exclude=* --delete-excluded -e "ssh" user#example.com:.config/ config/
But nothing works as I expected. The subfolders in the destination are deleted.
The man page for rsync has a section on "Include/Exclude Pattern Rules" which you should review. In particular, it looks like you are using ./ to refer to the directory relative of your shell's working directory which won't work the way you want.
Here is a relevant section of the documentation:
if the pattern starts with a / then it is anchored to a particular
spot in the hierarchy of files, otherwise it is matched against the
end of the pathname. This is similar to a leading ^ in regular
expressions. Thus /foo would match a name of "foo" at either the "root
of the transfer" (for a global rule) or in the merge-file's directory
(for a per-directory rule). An unqualified foo would match a name of
"foo" anywhere in the tree because the algorithm is applied
recursively from the top down; it behaves as if each path component
gets a turn at being the end of the filename. Even the unanchored
"sub/foo" would match at any point in the hierarchy where a "foo" was
found within a directory named "sub". See the section on ANCHORING
INCLUDE/EXCLUDE PATTERNS for a full discussion of how to specify a
pattern that matches at the root of the transfer.
The pattern you are using might be causing rsync to look for a folder literally called . which contains a file or folder called scripts or whatever.
Try replacing the relative-ish ./config/ part with / in your exclude patterns. That will anchor the pattern in the "root of transfer" which is the directory you are synchronizing. In other words, try this:
rsync -avz --min-size=1 --delete -e "ssh" user#example.com:./config/ ./config --exclude /scripts --exclude /logs --exclude /device.conf --dry-run
I hope that works. If it's not too late to check, please let me know if this worked for you.
I need to copy a directory and its content to a NEW directory.
Using: cp -r dir1/dir2 dir1/dir3
where dir3 is going to be created anew, it just copies the file(s) I have within dir2... It's like it copies dir2 naming it dir3.
Is there any way to copy dir2 into dir3?
I need to do it with a single command.
It's not really possible to do it in 1 command with cp. What you want is for dir3 to already exist when you do your copy. I'm not sure if there's a real reason why you have to do it in 1 command or not. You can certainly do it in one line.
mkdir dir/dir3;cp -r dir1/dir2 dir1/dir3
I'm doing an rsync trial where I have two files in the current folder:
share_2014_09_08.tar.gz share_2014_10_08.tar.gz
I want to rsync to a remote folder that contains three older files. I use the command:
rsync -avz --del ./*.tar.gz backups#pc01:/home/backups/monthly/
And the result int the destination folder is:
share_2014_03_05.tar.gz share_2014_09_08.tar.gz share_2014_10_08.tar.gz
As I understand it, this file:
share_2014_03_05.tar.gz
should have been deleted, so my question is what am I doing wrong.
You're passing to rsync a list of files that you want to synchronize. Not existing files are not passed, so delete option has no effects.
If you want to delete files, you'll have to synchronize the parent directory that contained removed files. You can use a include mask to only sync tarballs:
rsync -avz --include "*.tar.gz" --exclude "*" --del . backups#pc01:/home/backups/monthly/
for example, my directory like this:
lib
|
+--foo.so
+--bar.so -> bar.so.1.0.0.0
+--bar.so.1.0.0.0
Are these both ways always same?
The reason I ask this question is that I found unix will copy to real so file when I
cp -r lib /path/to/
new directory like this:
/path/to/lib
|
+--foo.so
+--bar.so
+--bar.so.1.0.0.0
The difference between so and ls -s is the difference between a file and a symbolic link. Symbolic links are like aliases to other files and operations on them result in changes in the linked files. When you do cp, it copies the linked file to the target directory with the link name as the file name, i.e., it reads the linked file when it opens the symbolic link to copy it. So lose the link and instead get a copy of the linked file. If you use -P option of the cp command you can preserve the symbolic link information.
cp -P lib /path/to/
I want to copy a complete directory content from /home/private_html/userx/ into the /home/private_html/usery/, the problem is that the directory userx contains few symlinks, and when using the cp it just skip them (skip occurs, if symlinks directs into a file, in case if it points into the directory, it just copy WHOLE directory instead...).
The command I was using looks following:
# cp -iprv /home/private_html/userx/ /home/private_html/usery/
Has anyone a solution to copy the directory "just as it is" into other place?
On FreeBSD, cp doesn't have an -r option. It does have -R, which should do what you want:
-R If source_file designates a directory, cp copies the directory and
the entire subtree connected at that point. If the source_file
ends in a /, the contents of the directory are copied rather than
the directory itself. This option also causes symbolic links to be
copied, rather than indirected through, and for cp to create spe‐
cial files rather than copying them as normal files. Created
directories have the same mode as the corresponding source direc‐
tory, unmodified by the process' umask.
Roland is right about the -R flag. You could also use a pair of tar-processes, which would make your command a little bit more system-independent:
tar -C /home/private_html/userx/ -cpf - . | tar -C /home/private_html/usery/ -epf -