0

My folder structure is as follows:

Photos
|
|---> 2015-08-23
      |
      |---> IMG_0019.JPG           // keep
            IMG_0019.JPG.json      // toss
            IMG_0020.JPG           // keep
            IMG_0020.JPG.json      // toss
            IMG_0021.JPG           // keep
            IMG_0021.JPG.json      // toss
            metadata.json          // toss

      2015-08-24
      |
      |---> IMG_0242.MP4           // keep
            IMG_0242.MP4.json      // toss
            metadata.json          // toss

      2015-08-25
      |
      |---> IMG_0243.MOV           // keep
            IMG_0243.MOV.json      // toss
            IMG_0243(1).MOV        // keep
            IMG_0243(1).MOV.json   // toss
            IMG_0244.PNG           // keep
            IMG_0019.PNG.json      // toss
            metadata.json          // toss

This goes on and on. My goal is to end up with a folder structure like so

Photos
|
|--> IMG_0019.JPG
     IMG_0020.JPG
     IMG_0021.JPG
     IMG_0242.MP4
     IMG_0243.MOV
     IMG_0243(1).MOV
     IMG_0244.PNG
     ...

I was looking into to doing a modified version of this article where I would loop through each directory and perform a regex to only grab file of value. Something like:

// get a list of all directories
// loop through directories
//     get all contents in directory
//     loop through all contents in directory
//        determine if file is of value (ie. MP4, PNG, MOV, JPG, etc.)
//        move selected file out to parent directory
//        remove directory

Any help would be appreciated

8
  • You have two subdirectories with the same name in the same parent directory? Also, the list of files that you have in your expected result don't match what you have marked as keep in the current structure. Mar 31 '20 at 2:28
  • Yes. This file structure is from google photos. So I would have both 2016-02-17 and 2016-02-17 #2.
    – Jghorton14
    Mar 31 '20 at 2:31
  • Sorry yes you are correct about the expected result vs marked as keep. I will fix that. I was just using that as an example
    – Jghorton14
    Mar 31 '20 at 2:32
  • You cannot have two folders in the same path with the same name. Is the second one actually called 2015-08-24 # 2? Mar 31 '20 at 2:35
  • Yes its actually called 2015-08-24 #2
    – Jghorton14
    Mar 31 '20 at 2:35
3

From your the structure in the expected result, you just want to keep the files that don't end in .json. This will do it:

cd Photos
find . -type f ! -name *.json -exec mv {} . \; && rm -rf 2015*

From inside of the Photos directory, you find the files that don't end in .json, move them into . or the current directory, and then delete the subdirectories and the files inside that you don't want. You can also use rm -ri 2015* so that it will prompt you before removing each directory or run the find and rm commands separately in case you change your mind before deleting the rest of the files and their directories.

2
  • This did the trick! Thanks
    – Jghorton14
    Mar 31 '20 at 3:03
  • @Jghorton14 You are welcome. Mar 31 '20 at 3:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.