查找名称匹配模式的目录并移动它们 [英] Find directories with names matching pattern and move them
本文介绍了查找名称匹配模式的目录并移动它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一堆目录,例如 001/
002/
003/
与其他名称中包含字母的目录混合在一起.我只想抓取所有带有数字名称的目录并将它们移动到另一个目录中.
I have a bunch of directories like 001/
002/
003/
mixed in with others that have letters in their names. I just want to grab all the directories with numeric names and move them into another directory.
我试试这个:
file */ | grep ^[0-9]*/ | xargs -I{} mv {} newdir
匹配部分有效,但最终将所有内容移动到 newdir
...
The matching part works, but it ends up moving everything to the newdir
...
推荐答案
我不确定我是否理解正确,但这里至少有一些帮助.
I am not sure I understood correctly but here is at least something to help.
结合使用 find 和 xargs 来操作文件列表.
Use a combination of find and xargs to manipulate lists of files.
find -maxdepth 1 -regex './[0-9]*' -print0 | xargs -0 -I'{}' mv "{}" "newdir/{}"
使用 -print0
和 -0
并引用替换符号 {}
使您的脚本更加健壮.它将处理出现不可打印字符的大多数情况.这基本上是说它使用