Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Move all photos from Google takeout into one directory (gist.github.com)
37 points by ljahier on Nov 2, 2022 | hide | past | favorite | 22 comments


Zsh:

  mkdir photos
  mv -v **/*.jpg **/*.mp4 photos
Bash:

  shopt -s globstar
  mkdir photos
  mv -v **/*.jpg **/*.mp4 photos
But I think I'd prefer them in a directory based on the EXIF date in the photo anyway:

Zsh:

  for i in **/*.jpg; do
    date=$(exif $i --machine-readable --tag=0x9003 | head -c 10 | tr : -)
    if [[ -n $date ]]; then
      mkdir -p $date
      mv -v $i $date
    else
      mkdir -p Unknown
      mv -v $i Unknown
      # (Check other EXIF values, e.g. DateTimeOriginal.)
    fi
  done


Hmm. You could really benefit from a couple of Linux commands that have “built in concurrency” for such use cases.

I think you could achieve the same result with just the find command:

`find ./takeout -iname “.jpg” -exec mv {} newdir`


If you're using Windows, then I'd probably just use a script like this as well instead of trying to figure out whatever the equivalent of find is.

If you're not using Windows, you should probably learn to use some basic shell commands.

Also, line 19 has a bug.

Also, readdir('foo', {withFileTypes: true}) will return a list of directory entries so you can check the type without doing a stat. Also, sync versions are easier.

    fs.readdirSync(DOCS_DIR, {withFileTypes: true}).forEach((file) => {
        if(file.name.startsWith('.') || !file.isDirectory()){ return; }
        ...
    });


I can't believe this is getting upvotes on HN, this is buggy and a simple oneliner can do it.


I didn't pretend to make a perfect code but just a functional code, and I share it because it can maybe help people who would encounter the same problem as me.


Don’t feel bad! To get better at something, you must be bad at it first. Consider improvements from feedback in this thread.


Thinking about this later, I realized that this script isn't even recursive. I think we've been Tom Sawyered, but I couldn't resist. I grabbed a Takeout zip I had hanging around and wrote this:

    import fs from 'fs';
    import path from 'path';

    copyMedia('Takeout');

    function copyMedia(dir) {
      fs.readdirSync(dir, {withFileTypes: true}).forEach((file) => {
        if (file.isDirectory()) {
          copyMedia(path.join(dir, file.name));
        } else {
          if (file.name.toLowerCase().endsWith('.jpg') || file.name.toLowerCase().endsWith('.mp4')) {
            fs.cpSync(path.join(dir, file.name), path.join('photos', file.name));
          }
        }
      });
    }
It uses copy instead of move, but it's fine.


Note: if you dump all your photos in one directory, prepare for opening that directory to be extremely slow.


.js ? FFS, really?

    find takeout -name ".jpg" -exec mv {} dest_dir


What’s wrong with a js script if author is comfortable with it? Is it a rule that everyone must know linux commands?


> What’s wrong with a js script

Not much more than is wrong with sending a nuclear-powered aircraft carrier to kill a mosquito

> Is it a rule that everyone must know linux commands?

Unix, and yes. Every system around you is built on it or something derived from it (either literally or in design).


Wrong comparison, I’ve taught enough people both UNIX and JS, and there are many of them who consider JS more easier than to grok and use than bash/unix. Not everyone wants to remember all the commands, and most of them use windows.

Also, no not every system around me requires me to learn unix, I can use my phone, microwave or even my gaming laptop(windows) without ever opening CLI. Unless Im at work or am I tinkering with my PI4s I don’t even have to touch CLI or even think about a unix command.


Here the author, I'm not working on windows but macos. I know, the script is not recursive but it's not the goal. The directory architecture of Google takeout > Google photos is one directory per day when there are pictures. In this directory, there are no other directory so the recursive is useless.

The goal was to make a script in quick time and I'm comfortable with nodejs that's why I chose it.


Yes, rules 8 and 14


From Unix handbook of how to make people hate the community? C’mon author was good enough to share a script for us to use, he might be just starting in programming, or may be its for a windows use case, don’t have to be so rude like the original commenter which is what I didn’t like.


I'll refer you to rules 0 and 1


There is nothing wrong with it per se, and no, you don’t have to know Linux commands to get by.

Sure, a Linux one liner might be “easier” to someone who knows Linux… but that doesn’t mean it’s “wrong.”


    ls -Recurse -Filter *.jpg | mv dest_dir


Out of everything in this thread, this was the most interesting to me. I didn't know you could pipe a list of files to mv. It makes sense, I guess I just never tried it because anything I would have used it for, I would have used find or xargs instead.


This is PowerShell and it doesn't work on *nix machines because ls and mv are native binaries.

Would work on Windows, would work on *nix if change it to

    gci -Recurse -Filter *.jpg | move dest_dir


Are the takeout filenames unique?

I think most cameras get to IMG_9999 and cycle back to IMG_0000


I only had a few thousand photos so I didn't get into this case. I modified to put in addition the name of the folder which is the date in the filename.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: