LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This 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


Reply
  Search this Thread
Old 03-20-2018, 05:37 PM   #1
jr_bob_dobbs
Member
 
Registered: Mar 2009
Distribution: Bedrock, Devuan, Slackware, Linux From Scratch, Void
Posts: 655
Blog Entries: 136

Rep: Reputation: 190Reputation: 190
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.).

Thank you.
 
Old 03-20-2018, 05:56 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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...)
 
Old 03-20-2018, 08:09 PM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
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)"

Last edited by BW-userx; 03-20-2018 at 08:40 PM.
 
Old 03-21-2018, 01:39 PM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,837

Rep: Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221
The following script does
list source files | exclude files | copy files
Code:
#!/bin/sh
excludir=/over/here
srcdir=/over/there
destdir=/new/dir

mkdir -p "$destdir" || exit
cd "$srcdir" || exit
find . -print | (
cd "$excludir" &&
while IFS= read -r f
do
  test -e "$f" || printf "%s\n" "$f"
done
) |
cpio -pd "$destdir"

Last edited by MadeInGermany; 03-22-2018 at 02:11 AM. Reason: raw read, cpio needs cwd=srcdir
 
1 members found this post helpful.
Old 03-21-2018, 06:10 PM   #5
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,157

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
Don't see cpio getting a run much any more ...
Maybe use print0 for safety ?.
 
Old 03-22-2018, 02:21 AM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,837

Rep: Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221Reputation: 1221
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.
 
Old 03-22-2018, 10:35 AM   #7
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,386

Rep: Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764Reputation: 2764
As a one-liner run from the ./over directory
Code:
mkdir new; cp ./there/$(join -v 2 <(cd here;find . ;cd ..) <(cd there;find . ;cd ..)) ./new/
Sorry - Epic fail with symlinks

Last edited by allend; 03-22-2018 at 11:24 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
copying files from home dir to another dir from another dir in a lower dir chomito44 Linux - General 5 10-19-2013 06:18 PM
Move files contained in source dir to destination dir, but not source dir itself unixunderground Linux - Software 3 09-20-2013 11:17 AM
tar dir and sub dir removing files but not existing not empty dir j-me Linux - General 2 08-12-2013 11:37 AM
How to copy 3 dir's contents to 1 dir by crontab? meandsushil Linux - Newbie 14 06-08-2010 09:44 AM
Command to display /dir, /dir/sub, /dir/sub/files knockout_artist Linux - Newbie 9 10-25-2007 02:57 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 10:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration