I've been wrestling with a 14TB+ video library for the last months trying to dedupe and sort all of it into production ready folder structure.

For the life of me I have not been able to find (spent 3 solid days with no sleep) a script that will sort a folder of video files, using their dimensions, into Portrait and Landscape folders.

The closest I've found is this -

shopt -s nullglob

for f in *.{mp4,MP4,mov,MOV,m4v,M4V}
        height=`mdls -raw -name  kMDItemPixelHeight "$f"`
        width=`mdls -raw -name  kMDItemPixelWidth "$f"`
        mkdir -p "${height}x${width}"
        mv "$f" "${height}x${width}"/
        printf "File: $f\n"     
        printf "> Dimensions: $height x $width \n\n"

printf "All done! \n"

Which gets me closer by sorting them into folders based on dimensions, but it's not what I'm trying to achieve.

Alternatively I found this script for images using Imagemagick, (which I installed via Homebrew) it does read the files, though it moves them ALL into the portraits directory. I'm assuming the variables may be different to extract height and width from a video file?

mkdir -p portraits
mkdir -p landscapes
for f in ./*.mp4
  r=$(identify -format '%[fx:(h/w)]' "$f")
  if [[ r < 1.0 ]] 
      echo "Portrait detected."
  mv "$f" ./portraits/
  elif  [[ r > 1.0 ]]
  echo "Landscape detected."
      mv "$f" ./landscapes/

If ANYONE OUT THERE can help me with this, it's worth a PayPal to me. I know it's quick work for the right mind.

The same, if someone has another solution using folder actions or automator or ANYTHING else. I'm running Mac OS with terminal.

Much appreciated!

[email protected]


1 Answer 1


You can use the test function provided by fish to compare the two dimensions:

for i in *.{mp4,MP4,mov,MOV,m4v,M4V}
  if test (mdls -raw -name kMDItemPixelHeight $i) -gt (mdls -raw -name kMDItemPixelWidth $i)
     mv $i portrait/
     mv $i landscape/

bash probably provides the same functionality, but I'm more familiar with fish.

  • OH MY GOD. JOY! It worked! Thank you so much! How would I modify this so it's recursive to all sub dirs but brings the files up the main dir putting them ALL in the portrait and landscape folders? I have no problem using FISH, but if anyone can provide insight on how to convert this to BASH, I'd like to know only because I've been trying to figure this out for months. Commented Nov 23, 2020 at 21:03
  • Use for i in **.{mp4,MP4,mov,MOV,m4v,M4V}
    – Joy Jin
    Commented Nov 24, 2020 at 2:48
  • I will convert this to bash when I have time to learn about bash syntax.
    – Joy Jin
    Commented Nov 24, 2020 at 2:52

You must log in to answer this question.

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