Compare folders and find missing files

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Compare folders and find missing files

P. Echols
Greetings all,

is there a way to get "find" to return only when a file is NOT found?

I am attempting to list the files on a flash drive (/media/project/) ,
but ONLY, if those files don't exist in a particular folder in my
computer (~/Documents/sub-tree/).  If I cd to the sub-tree folder, I
can run the following:

find /media/project/ -type f -exec basename {} \; | while read fname;
do find -name "$fname"; done

This will list the path and file name for all the files that are
found.  But not the ones that are not found.  Any thoughts how I might
accomplish this?

Thanks
-- PE

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: Compare folders and find missing files

Chris Green
On Wed, Mar 25, 2020 at 12:40:09AM -0700, P. Echols wrote:

> Greetings all,
>
> is there a way to get "find" to return only when a file is NOT found?
>
> I am attempting to list the files on a flash drive (/media/project/) ,
> but ONLY, if those files don't exist in a particular folder in my
> computer (~/Documents/sub-tree/).  If I cd to the sub-tree folder, I
> can run the following:
>
> find /media/project/ -type f -exec basename {} \; | while read fname;
> do find -name "$fname"; done
>
> This will list the path and file name for all the files that are
> found.  But not the ones that are not found.  Any thoughts how I might
> accomplish this?
>
Try 'diff', it can compare directories and tell you which files are
only in one or the other directory, you'll need to set a few options to
get the level of reporting you want.

--
Chris Green

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: Compare folders and find missing files

Peter Flynn
On 25/03/2020 09:02, Chris Green wrote:

> On Wed, Mar 25, 2020 at 12:40:09AM -0700, P. Echols wrote:
>> Greetings all,
>>
>> is there a way to get "find" to return only when a file is NOT found?
>>
>> I am attempting to list the files on a flash drive (/media/project/) ,
>> but ONLY, if those files don't exist in a particular folder in my
>> computer (~/Documents/sub-tree/).  If I cd to the sub-tree folder, I
>> can run the following:
>>
>> find /media/project/ -type f -exec basename {} \; | while read fname;
>> do find -name "$fname"; done
>>
>> This will list the path and file name for all the files that are
>> found.  But not the ones that are not found.  Any thoughts how I might
>> accomplish this?
>>
> Try 'diff', it can compare directories and tell you which files are
> only in one or the other directory, you'll need to set a few options to
> get the level of reporting you want.

You could test for the count of files found being zero, eg

$ find /media/project/ -type f -exec basename {} \; | while read fname;
do if [ `find ~/Documents/sub-tree -name "$fname" | wc -l` -eq 0 ]; then
echo $fname not there; fi; done

But diff may be faster.

Peter

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: Compare folders and find missing files

Karl Auer
In reply to this post by P. Echols

> is there a way to get "find" to return only when a file is NOT found?


Read "man find". Look under "OPERATORS" and check out the "!" operator.
It inverts the sense of a test.

So this:

   find -name "xxx*" -print

will print all filenames matching "xxx*", but this

   find ! -name "xxx*" -print

will print all filenames NOT matching "xxx*".
Regards, K.
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Karl Auer ([hidden email])
http://www.biplane.com.au/kauer
http://twitter.com/kauer389

GPG fingerprint: 2561 E9EC D868 E73C 8AF1 49CF EE50 4B1D CCA1 5170
Old fingerprint: 8D08 9CAA 649A AFEF E862 062A 2E97 42D4 A2A0 616D



--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: Compare folders and find missing files

Ken D'Ambrosio
In reply to this post by Peter Flynn
On 2020-03-25 06:12, Peter Flynn wrote:
> On 25/03/2020 09:02, Chris Green wrote:
>> On Wed, Mar 25, 2020 at 12:40:09AM -0700, P. Echols wrote:

NB: the following might give false positives, as the unqualified
filename is being searched for, and, depending on your hierarchy, you
might have multiple directories with files with the same name.
> $ find /media/project/ -type f -exec basename {} \; | while read
> fname; do if [ `find ~/Documents/sub-tree -name "$fname" | wc -l` -eq
> 0 ]; then echo $fname not there; fi; done

My approach, while less elegant, is more explicit, and doesn't
repeatedly walk the compared tree:
cd /media/project && find . -type f | while read fname; ls
~Documents/sub-tree/$fname > /dev/null 2> /dev/null || echo "$fname not
there."

Note that there will be a superfluous -- but functionally noop -- "./"
in the path shown of any missing files.

None of which invalidates the point about diff possibly being the most
efficient method.

$.02,

-Ken

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Wups: Re: Compare folders and find missing files

Ken D'Ambrosio
D'oh.  I checked my one-liner, noted I had a goof or two... and then
sent it without fixing.
Gonna be that type of day, I'm afraid.  New-and-improved (a/k/a
"functional") below.

On 2020-03-25 08:52, Ken D'Ambrosio wrote:
[...]
less elegant, is more explicit, and doesn't
> repeatedly walk the compared tree:
cd /media/project && find . -type f | while read fname; do ls
~Documents/sub-tree/$fname > /dev/null 2> /dev/null || echo "$fname
not there."; done

>
> Note that there will be a superfluous -- but functionally noop -- "./"
> in the path shown of any missing files.
>
> None of which invalidates the point about diff possibly being the most
> efficient method.
>
> $.02,
>
> -Ken

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users
Reply | Threaded
Open this post in threaded view
|

Re: Wups: Re: Compare folders and find missing files

Francis Grizzly Smit


On 25/03/2020 23:54, Ken D'Ambrosio wrote:
D'oh.  I checked my one-liner, noted I had a goof or two... and then sent it without fixing.
Gonna be that type of day, I'm afraid.  New-and-improved (a/k/a "functional") below.

On 2020-03-25 08:52, Ken D'Ambrosio wrote:
[...]
less elegant, is more explicit, and doesn't
repeatedly walk the compared tree:
cd /media/project && find . -type f | while read fname; do ls
~Documents/sub-tree/$fname > /dev/null 2> /dev/null || echo "$fname
not there."; done

cd /media/project

for f in *

do

     if [ ! -e ~Documents/sub-tree/"$f" ]

     then

         echo "$f missing."

     fi

done

is good for one level, but I'd use diff -r /media/project/ ~Documents/sub-tree/
for a more complex (deeper) case



Note that there will be a superfluous -- but functionally noop -- "./"
in the path shown of any missing files.

None of which invalidates the point about diff possibly being the most
efficient method.

$.02,

-Ken

--
   .~.     In my life God comes first....
   /V\         but Linux is pretty high after that :-D
  /( )\    Francis (Grizzly) Smit
  ^^-^^    http://www.smit.id.au/

--
ubuntu-users mailing list
[hidden email]
Modify settings or unsubscribe at: https://lists.ubuntu.com/mailman/listinfo/ubuntu-users