查找名称与模式匹配的目录并将其移动 [英] 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
并引用替换符号{}
使脚本更加健壮.它可以处理存在不可打印字符的大多数情况.这基本上表示它使用\0
字符定界符而不是\n
通过行.
Using -print0
and -0
and quoting the replacement symbol {}
make your script more robust. It will handle most situations where non-printable chars are presents. This basically says it passes the lines using a \0
char delimiter instead of a \n
.
这篇关于查找名称与模式匹配的目录并将其移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!