原創技術
# 命令操作 # mac

使用命令-批次所有指定檔名複製到新的資料夾

2,853 字6 分鐘0 瀏覽0 按讚

摘要

本文介紹了在macOS上使用終端命令批次複製指定檔名照片的方法。針對婚禮拍攝後兩千多張照片中挑選部分編號檔案的需求,提供了兩種方案:一是直接使用檔名陣列複製,二是動態讀取逗號分隔的編號檔案並轉換為檔名格式。兩種方案均涉及建立目標資料夾、設定777許可權、使用find與cp命令遞迴搜尋並複製檔案。該方法適用於需要從大量檔案中精確選取並轉移指定檔案的場景,未來計劃整合到命令列工具或軟體中。

上次拍完婚禮後,有兩千多張的照片,今天挑了一些,只記了一下照片的名稱中的數字,本以為mac的檔案搜尋可以做到用英文逗號分隔進行快速搜尋,誰知道居然做不到,那麼這個時候就要動腦想一下了。

未在Windows上測試過

一定要在sudo的許可權下使用命令


準備工作 1. *檔案路徑*:

  • 我的照片儲存在 /Volumes/婚礼/DCIM(包含子資料夾)。
  • 目標是將匹配的檔案複製到 /Volumes/婚礼/DCIM/matched_photos。
  • *編號列表*: - 我有一個逗號分隔的編號列表,儲存在文字檔案 /Volumes/婚礼/DCIM/numbers.txt 中,內容如下:
0182,0178,0168,0165,0163,...
  • 需要將這些編號轉換為檔名格式:IMG_编号.CR2(例如 IMG_0182.CR2)。
  1. 環境:
  • macOS 系統(我用的是 zsh shell,macOS 預設 shell)。

方案 1:直接使用檔名列表

1. 準備編號檔案

確保你的編號列表已經儲存為文字檔案。如果沒有,可以手動建立:

  • 開啟“文字編輯”(TextEdit)。
  • 輸入編號,例如:
0182,0178,0168,0165,0163,...
  • 儲存為 /Volumes/婚礼/DCIM/numbers.txt(確保儲存為純文字格式,避免富文字)。

2. 將編號轉換為檔名格式

我的照片檔名格式是 IMG_XXXX.CR2,所以需要將編號加上字首 IMG_ 和字尾 .CR2。可以用文字編輯器的“查詢和替換”功能:

  • 查詢 ,,替換為 .CR2,IMG_。
  • 手動在開頭加 IMG_,結尾加 .CR2。
  • 示例結果(部分):
IMG_0182.CR2,IMG_0178.CR2,IMG_0168.CR2,...

3. 使用終端命令搜尋和複製檔案

直接將檔名列表寫入命令,並設定許可權:

開啟終端

  • 按 Command + 空格,輸入“終端”(Terminal),回車。

輸入命令

將以下命令複製到終端並執行(這裡只展示部分檔名,完整列表包含 151 個):

mkdir -p /Volumes/婚礼/DCIM/matched_photos && chmod 777 /Volumes/婚礼/DCIM/matched_photos && numbers=(IMG_0182.CR2 IMG_0178.CR2 IMG_0168.CR2 IMG_0165.CR2 IMG_0163.CR2) && for number in $numbers; do find /Volumes/婚礼/DCIM -type f -name "$number" -exec cp {} /Volumes/婚礼/DCIM/matched_photos/ \; ; done && chmod 777 /Volumes/婚礼/DCIM/matched_photos/*

命令說明:

  • mkdir -p /Volumes/婚禮/DCIM/matched_photos:建立目標資料夾。
  • chmod 777 /Volumes/婚禮/DCIM/matched_photos:為目標資料夾設定 777 許可權。
  • numbers=(...):定義檔名陣列,直接使用轉換後的檔名。
  • find /Volumes/婚禮/DCIM -type f -name "$number":在 /Volumes/婚禮/DCIM 及其子資料夾中搜尋精確匹配的檔名。
  • -exec cp {} /Volumes/婚禮/DCIM/matched_photos/ ;:將找到的檔案複製到新資料夾。
  • chmod 777 /Volumes/婚禮/DCIM/matched_photos/*:為複製後的所有檔案設定 777 許可權。

方案 2:動態讀取 numbers.txt 檔案

如果你的編號列表可能會變化,或者你希望更靈活地管理檔案,可以利用 numbers.txt,動態讀取編號並生成檔名。

1. 準備編號檔案

確保 /Volumes/婚禮/DCIM/numbers.txt 已存在,內容為逗號分隔的編號:

0182,0178,0168,0165,0163,...

2. 使用終端命令讀取和處理

以下命令會讀取 numbers.txt,將編號轉換為檔名格式並執行搜尋,同時設定許可權:

3.開啟終端

  • 按 Command + 空格,輸入“終端”(Terminal),回車。

4.輸入命令

將以下命令複製到終端並執行:

mkdir -p /Volumes/婚礼/DCIM/matched_photos && chmod 777 /Volumes/婚礼/DCIM/matched_photos && numbers=($(cat /Volumes/婚礼/DCIM/numbers.txt | tr ',' ' ')) && for number in $numbers; do find /Volumes/婚礼/DCIM -type f -name "IMG_${number}.CR2" -exec cp {} /Volumes/婚礼/DCIM/matched_photos/ \; ; done && chmod 777 /Volumes/婚礼/DCIM/matched_photos/*

命令說明:

  • mkdir -p /Volumes/婚禮/DCIM/matched_photos:建立目標資料夾。
  • chmod 777 /Volumes/婚禮/DCIM/matched_photos:為目標資料夾設定 777 許可權。
  • numbers=($(cat /Volumes/婚禮/DCIM/numbers.txt | tr ',' ' '))
  • cat 讀取 numbers.txt。
  • tr ',' ' ' 將逗號替換為空格,生成 zsh 陣列。
  • for number in $numbers; do ...:遍歷每個編號(例如 0182)。
  • -name "IMG_${number}.CR2":動態生成檔名,例如 IMG_0182.CR2。
  • -exec cp {} /Volumes/婚禮/DCIM/matched_photos/ ;:將找到的檔案複製到新資料夾。
  • chmod 777 /Volumes/婚禮/DCIM/matched_photos/*:為複製後的所有檔案設定 777 許可權。

優點:

  • 如果需要更改編號,只需編輯 numbers.txt,無需修改命令。
  • 更靈活,適合動態需求。

常見問題

1. 路徑錯誤

  • 檢查磁碟是否掛載:
ls /Volumes/婚礼/DCIM

. 檔名不匹配

  • 檢視實際檔名:
find /Volumes/婚礼/DCIM -type f | head -n 10

3. 子資料夾未包含

  • find 預設遞迴搜尋子資料夾。可以測試單個檔案:
find /Volumes/婚礼/DCIM -type f -name "IMG_0182.CR2"

4. numbers.txt 讀取問題

  • 檢查檔案內容:
cat /Volumes/婚礼/DCIM/numbers.txt

5. 許可權問題

  • 如果仍無法檢視或編輯檔案,檢查許可權:
ls -l /Volumes/婚礼/DCIM/matched_photos

資料夾如果不存在的話,請手動建立一下,我再測試的時候是已經有了資料夾的,沒有資料夾的情況還沒有測試過

後期計劃

後期準備直接做個命令列執行的工具,或者是直接整合軟體,雖然現在已經有很多的軟體支援這些功能了

留言載入中…
相關文章

相關文章