如何找到文件名稱(或正文)中包含字串的一堆文件,然後將所有這些文件移到特定資料夾?

如何找到文件名稱(或正文)中包含字串的一堆文件,然後將所有這些文件移到特定資料夾?

假設- 如果我想將幾個獨立目錄中的檔案名稱中包含“heavengames”一詞的每個HTML 檔案(作為第二個問題,正文中包含“heavengames”一詞的每個HTML 檔案)移動到名為的新目錄,該怎麼辦?

答案1

要移動名稱中包含該單字的檔案:

find /path/to/dir1 /path/to/dir2 /and/so/on -type f -iname "*heavengames*" \
-exec mv -t /path/to/heavengames-threads {} \+

要移動正文中包含 word 的檔案:

find /path/to/dir1 /path/to/dir2 /and/so/on -type f -exec grep -q heavengames {} \; \
-exec mv -t /path/to/heavengames-threads {} \+

附:要檢查一切是否正確,請在第一次運行時添加echo之前。mv

答案2

在 zsh 或 bash ≥4 中,依檔案名稱:

mkdir heavengames-threads
mv **/*heavengames*.html heavengames-threads/

為了獲得更大的靈活性(例如,要重新建立目錄層次結構,請尋找zmv(有這個網站上有很多例子)。

用於grep搜尋文件內容。使用最新版本的 GNU 實用程式(即在非嵌入式 Linux 或 Cygwin 上):

grep -RZ heavengames . | xargs -0 mv -t heavengames-threads/

對於更便攜的命令,請使用find.看拉許的回答

相關內容