Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have bash script that utilizes the Mac based mdls command in a shell script to generate a metadata report for media files. When the script works the output looks like this:

1) [./]  
- Duration: 22.03  
- Dimensions: 480 X 640 pixels   
- Codec: ( "H.264" )  

Sometimes I get null in the results for all files in the report:

1) [./]   
- Duration: (null)   
- Dimensions: (null) X (null) pixels  
- Codec: ( null )     

Here is my script that generates the report:

cd "path_to_folder"
while IFS= read -r -d $'\0' file; do
  duration=`mdls -name kMDItemDurationSeconds "$file" | cut -d "=" -f 2 `
  duration=`printf "%.2f" $duration;`
  pixel_height=`mdls -name kMDItemPixelHeight "$file" | cut -d "=" -f 2`
  pixel_width=`mdls -name kMDItemPixelWidth "$file" | cut -d "=" -f 2`
  codec=`mdls -name kMDItemCodecs "$file" | cut -d "=" -f 2`
  temp="$i) [$file]\n- Duration: $duration\n- Dimensions: $pixel_width X $pixel_height    pixels\n- Codec: $codec\n"
  i=$((i + 1))
done < <(find .  \( -iname \*.m4v -o -iname \*.mov -o -iname \*.m4r -o -iname \*.m4a \)  -print0 )

 echo -e  "\n[Report]\n"$metaDataOutput 

Any idea what I might be doing wrong here? Why the nulls?

share|improve this question
So you say that sometimes, it just works for all files and then, it doesn't? Or is this only happening to specific files? – slhck Apr 7 '12 at 18:58
up vote 0 down vote accepted

Perhaps the file is excluded from spotlight's index? Or perhaps it's on a hard drive or network share that doesn't support spotlight indexing?

If none of those are the case, it sounds like your spotlight database is broken. There are some extremely complicated optimisations in spotlight to keep it's performance impact to a minimum, and sometimes it breaks.

You can use mdutil to find out the status of a drive's spotlight database (use /Volumes/other_disk if you don't want to apply these to the boot disk):

sudo mdutil -s /

You can delete/flush the database on a particular disk by running:

sudo mdutil -E /

Spotlight will then rebuild the index when it feels like it (probably when your system is idle). You can bump it to re-index right away with:

sudo mdutil -i on /

Depending how many files you have on your system, it could take many hours to rebuild the index. You can follow progress in the GUI's spotlight search icon.

See man mdutil for more info.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .