How to check which files are not in iTunes library, but are in iTunes folder? This is a question from Wil Wheaton's google+, but I would like to get an answer too, stackexchange style.

Suppose you have all your music in a folder called /iTunes/Music. When you add files to iTunes, it puts them inside this folder, so it looks like this: /iTunes/Music/artist/album/track

Suppose you recently eliminated about 10GB of music from your iTunes library, but stupidly left those files inside /iTunes/Music. You have the entire thing backed up and pristine on an external drive, so you don't need to keep that music in that folder.

Here's the question: you want to write a script that will compare your iTunes library to the contents of /iTunes/Music, identify files that are in /iTunes/Music but not the iTunes Library file, and  move them to a new folder (probably in /tmp) so you can take a look, then delete them.

One possible solution  would be to write  a script which puts the output of ls (with appropriate flags) into a file, then diff that file with the contents of Library.xml, and move the resulting files into a new directory. Is it possible to do this?

Maybe there is already an Applescript which can be used to achieve this?
You could move the location of your iTunes Media folder, which will only move those files actually in the iTunes library.

It may take a while, even if the source and destination are both on the same drive (it's still got to tell the OS to move each file individually), but should leave behind any files that aren't in the library.

The iTunes Media folder location is set in iTunes Preferences, under the Advanced tab. This will of course only work if you have "Keep iTunes Media folder organized" checked (which judging by your question is the case).

I would suggest something as simple as changing just the name of the folder it's currently set to (i.e. keep it somewhere in your home folder, or whatever drive it's currently on), and then move it back after you've cleaned out the 'orphaned' files.