copy files only in dir 2 that are not also in dir 1 into a new dir
Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
copy files only in dir 2 that are not also in dir 1 into a new dir
Sorry for the wordy title. Could not figure out how to reduce it further without removing all meaning.
Consider a directory tree at /over/here, which contains regular files, sym links and directories, which themselves contain regular files, sym links and directories, etc.
Consider the same sort of directory tree at /over/there
My task is to create a directory /new/dir which contains everything in the second directory that is not also in the first, preserving the relative directory structure.
I think this is a simple task but I just cannot figure out how to do it with the usual tools (diff, sort, sed etc.).
Maybe you could try to clone /over/there to /new/dir with rsync using exclude option
Now the job becomes to just build the exclude list (it accepts wildcards...)
you could write a script to copy the necessary from the first source , or command line copy first source then a script to have it go into the second source, using if statement to check first if the same file is already there or not, if it is there then do not copy that from the second source, if it is not there then copy it over into the same destination.
though in reality if it is a copy or move, I'd just copy or move the first source over then the second over top of it, it will still end result be no dups because it will just copt over top of what is already there and add the what is not already as well.
but a script would be something like this. it is not testing for or coping links. it's just a basic working script to test if the file is already there or not, and report back the results of its findings.
Code:
#!/bin/bash
working_dir="/home/userx/Documents"
destanation=/home/userx/Documents
while read f ;
do
c=$f
xpath=${c%/*}
xbase=${c##*/}
xfext=${xbase##*.}
xpref=${xbase%.*}
path=${xpath}
pref=${xpref}
ext=${xfext}
#covers base and sub dir
#replaces source path with destanation then
#adds the sub dir to match source if a match is there.
search=${path/$working_dir/$destanation}
if [[ "$path"/"$pref" == "$search"/"$pref" ]] ; then
{
echo "Match"
}
else
{
echo "Not Match"
}
fi
done <<<"$(find "$working_dir" -type f)"
You mean special file names?
I have edited my last post, now the read handles leading/trailing space characters and backslashes in filenames.
Only newline characters in filenames are still not handled, that would denote besides a -print0 in find an adaption in read, printf, cpio.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.