Compare folders and find missing files

Peter Flynn peter at silmaril.ie
Wed Mar 25 10:12:47 UTC 2020


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




More information about the ubuntu-users mailing list