如何刪除資料夾但只保留選定的檔案?

如何刪除資料夾但只保留選定的檔案?

我的資料夾結構如下:

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

這種情況一直持續下去。我的目標是最終得到像這樣的資料夾結構

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

我正在考慮做一個修改版本文章 我將循環遍歷每個目錄並執行正則表達式以僅獲取有價值的文件。就像是:

// 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

任何幫助,將不勝感激

答案1

從預期結果的結構來看,您只想保留不以.json.這將做到這一點:

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

從 Photos 目錄內部,找到不以 結尾的文件.json,將它們移到.目前目錄中,然後刪除不需要的子目錄和文件。您也可以使用rm -ri 2015*它,以便在刪除每個目錄之前提示您,或單獨執行findrm命令,以防您在刪除其餘檔案及其目錄之前改變主意。

相關內容