需求
將所有子目錄裏的txt文件移動到當前目錄。
D:\INSTALLER\2
│ 321.txt
│
└─新建文件夾
│ 1.txt
│
└─新建文件夾
fds.txt
答案
find . -mindepth 2 -name “*.txt” | xargs -I file mv file ./
原理:
find
命令便令當前目錄,通過-mindepth 2
排除當前層級。
xargs
命令將find
的結果傳遞給mv命令,通過-I file
參數指定輸入的別名爲file
。該段命令可替換爲| xargs mv -t ./
,mv -t
顛倒了原路徑和目標路徑,免除了-I
參數,但若文件名含有空格,則不能正常執行。