需求
将所有子目录里的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
参数,但若文件名含有空格,则不能正常执行。