2

When exporting from Photos.app with settings that preserve the album & folder structure, every album folder contains dated folders for every date represented in that album, with the actual images inside those. An example export for this question gives this structure...

Export/
    Colour/
        11 November 2005/
            img_0440.jpg
        13 November 2005/
            pb130006_1_1.jpg
    Life/
        Creatures/
            12 November 2005/
                img_0453.jpg
        People/
            9 November 2005/
                img_0174.jpg
            10 November 2005/
                img_0181.jpg
            14 November 2005/
                pb140009.jpg
        Plants/
            10 November 2005/
                img_0404.jpg
                img_0408.jpg
            13 November 2005/
                img_0477.jpg
            14 November 2005/
                img_0625.jpg

What can I run in Terminal to traverse the contents of Export & move all found files up one directory level?

The desired result on my example would be...

Export/
    Colour/
        img_0440.jpg
        pb130006_1_1.jpg
    Life/
        Creatures/
            img_0453.jpg
        People/
            img_0174.jpg
            img_0181.jpg
            pb140009.jpg
        Plants/
            img_0404.jpg
            img_0408.jpg
            img_0477.jpg
            img_0625.jpg

A solution that works in bash, because I do a lot of my CL stuff in BBEdit which is bash only, or fish because that's my preferred shell in Terminal, would be preferred, but I'll work with other shells.

2
  • 1
    It's not my usual practice to cross-post, but this time I was a bit at a loss as to the most appropriate to post in. Would you kindly suggest which is best for this question? Update... deleted the SuperUser post following bmike's edit & comment here. – Pedro Nov 29 '20 at 9:57
  • 1
    So, I’m going to at least remove the recursive, this seems more iterative, but regardless which approach a program takes, the result is clear without needing to specify a technique to flatten the hierarchy – bmike Nov 29 '20 at 9:58
3

This is untested: remove the echo if it looks right:

find ./Export -name '*.jpg' -print0 |
  xargs -0 sh -c 'for file; do echo mv "$file" "${file%/*}"/..; done' sh

The odd-looking trailing sh is required because it gets set as $0 in the shell body, so the rest of the arguments fed to it by xargs can be easily iterated.

This will leave the empty directories. To list empty directories:

find . -type d -empty -print

To delete empty directories:

find . -type d -empty -print -delete

To not rely on the file extension:

find ./Export -type f -print0 |
  xargs -0 sh -c '
    for file; do
      case "$(file "$file")" in
        *"JPEG image data"*) echo mv "$file" "${file%/*}"/..;;
      esac
    done
  ' sh
3
  • 2
    Another way to list empty directories is with find . -type d -empty -print and then delete them with find . -type d -empty -delete. – Graham Miln Nov 29 '20 at 15:32
  • 1
    @GrahamMiln, I've been using this to get rid of empty directories ;) – Pedro Nov 29 '20 at 15:37
  • That works, but it depends on the the files all being JPEGs with lowercase extensions. Using '*.*' also seems to work, so I'll try it with some text exports with a greater variety of file types & names. – Pedro Nov 29 '20 at 15:53

You must log in to answer this question.

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