Log in

No account? Create an account
An author of no particular popularity

Jay Lake
Date: 2007-08-24 05:47
Subject: [help] Help on stupid Mac tricks
Security: Public
Tags:help, tech
I need some help with finding files on a Mac. While I generally consider myself a power user, this one has me a bit stumped.

Say I have two drives, with files like so:

Drive 01Drive 02
File AFile A
File CFile B
File DFile C
File FFile D

How do I search in the Finder, or Spotlight, or on the Darwin command line, to locate those files on Drive 02 which are not also on Drive 01? Ie, in the above array, I want to detect that Drive 02\File B exists, and is not present on Drive 01. I don't care about Drive 1\File F...I already know where it is.

(Yes, I messed up a backup-and-restore a while back. I caught it before I'd wiped the now-displaced data, but I'm finally getting to the point where I need to retrieve it from the idled backup disk so I can put that back into service.)
Post A Comment | 11 Comments | | Flag | Link

User: ex_chrisbil
Date: 2007-08-24 13:05 (UTC)
Subject: (no subject)
Are you Unix savvy, Jay? Reason being that I have vague ideas in my head of doing some funky grep n' changing the standard output to a text file, then appending said text file through another search to remove... or making another, and scripting it to rm any that are on both lists...

Am under the kosh a little right now, but if you don't need this instantly, I might be able to help over the weekend.
Reply | Thread | Link

Jay Lake
User: jaylake
Date: 2007-08-24 13:11 (UTC)
Subject: (no subject)
That would be terrific. I'm a bit of a monkey on the command line, but not hopeless.
Reply | Parent | Thread | Link

Peter Hollo
User: frogworth
Date: 2007-08-24 14:53 (UTC)
Subject: (no subject)
I guess you would do something like:
1) get a list of the paths of all the files on Drive 02. I assume the directory structure of the two drives is the same under the root, or under whatever folder you're putting everything...
Not 100% for OSX but on a Linux system you could just go:
$ find * > results.txt
and you'd have a list of file paths, one per line, in your results file.

2) Then a really quick and easy hack would be to open up a text editor and take each line and turn it into a command like this:
if ![[ -e /folder/filename ]] ; then echo '/folder/filename'; fi
and then append a line at the top like:
and make that file executable. I THINK this will then let you run the file (obviously in the equivalent folder on the other drive) as a shell script, and it should echo only the paths of the filenames it doesn't find. You can once again send the output to a file and that should be your list.

I haven't tried this out exactly, but maybe a hack like that would work? (I may have some details wonky, as IANAP - just a tinkerer). Someone can no doubt come up with something much less ugly...
Reply | Parent | Thread | Link

Peter Hollo
User: frogworth
Date: 2007-08-24 15:00 (UTC)
Subject: (no subject)
As a PS:
Of course you could do some kind of "for do ... od" loop to loop through all the filenames in the results.txt file, but I'm not sure how to tell it to do each line. You probably have a better idea than me!
Hopefully this idea is of enough use for you to put something together. Otherwise some better brains trust will come along soon enough :)
Reply | Parent | Thread | Link

Alexander B. L.
User: aleksandyr
Date: 2007-08-24 15:03 (UTC)
Subject: (no subject)
The question I have for you is if you care in which directory File B is in --- that is to say, if

Drive 01\Directory A\File A
Drive 02\Directory B\File A

is File A missing?

Let's say, for the point of argument, it is. This isn't the most elegant solution, but I just tested it with some toy directories, and it worked:

cd /Drive01
find . > patterns
cd /Drive02
find . | grep -v -f /Drive01/patterns

What this does is get the list of all files on Drive01 and puts it into the "patterns" file: it then takes the list of all files on Drive02 and checks to see if there is a matching entry (hence the -f /Drive01/patterns) in Drive01. If there is no matching entry (hence -v, to invert) it will print the line.

It's important to do the directory changes, because otherwise find will output a list of /Drive01/... and /Drive02/... which grep won't be smart enough to deal with.
Reply | Parent | Thread | Link

Alexander B. L.
User: aleksandyr
Date: 2007-08-24 15:05 (UTC)
Subject: (no subject)
In my defense, frogworth's solution wasn't up yet :)

His is going to be a lot faster. The advantage of my solution is that if you only care about filenames, and not paths, it's easier (possible?) to adapt.
Reply | Parent | Thread | Link

User: brni
Date: 2007-08-24 15:25 (UTC)
Subject: (no subject)

You could try something like this:

on drive 1:
cd [folder you're checking]
ls -Rp1 | sort -d > contents1

on drive 2:
cd [folder you're checking]
ls -Rp1 | sort -d > contents2

That should give you 2 files with the contents of the folder and it's subdirectories sorted in phone-directory format. Folders will be indicated with a trailing "/". You can edit out the folders using an editor (you can also write an awk or sed script if you want, but my awk-fu isn't good enough to do that without a lot more caffeine and some poking around in the book). If you want to keep the directory structure intact, eliminate the sort -d part.


diff -w contents1 contents2

(-w means ignore white space)

You may need to tweak the files a bit, depending on the nature of the data, and of course you can also look at the man files:

man ls
man sort
man diff


One thing - that doesn't give you any indication of versions. If you have a file that you updated on disk 1 and want to know whether you have the updated version on disk 2, this won't tell you that. You can do a more labor intensive task and get output as such:

ls -lRp1 | sort -d > contents[12]

to generate your files. Output would be verbose, showing last-update and file size. If file size is mismatched, that would indicate different versions. Again, some awk-fu might automate this.
Reply | Parent | Thread | Link

User: ex_chrisbil
Date: 2007-08-26 01:04 (UTC)
Subject: (no subject)
This is good stuff. I bow to clearly superior knowledge, so unless I hear otherwise I'll assume this is the better route, or the rsync option (I use rsync to synchronise data off site - it's very reliable).

Give me a yell, Jay, if you didn't find the solutions in all these comments. I may well be able to script something, and if I can't then I work with a guy who can tell you how POP1, 2 *and* 3 work. He's that [geek]cool!
Reply | Parent | Thread | Link

User: connatic
Date: 2007-08-24 16:05 (UTC)
Subject: (no subject)
diff -rq directory1 directory2

In the terminal, you can drag files or directories form the Finder to get the full pathname (grab the folder icon at the top of the window).
Reply | Thread | Link

User: autopope
Date: 2007-08-24 17:07 (UTC)
Subject: (no subject)
rsync is your friend. (NB: UNIX terminal skillz required. Google on it ...)
Reply | Thread | Link

Rose Fox
User: rosefox
Date: 2007-08-24 18:08 (UTC)
Subject: (no subject)
If the UNIX stuff isn't your thing, you can download Transmit FTP, FTP to, and then run Synchronize on the two drives.
Reply | Thread | Link

my journal
January 2014
2012 appearances