查找名称匹配模式的目录并移动它们 [英] Find directories with names matching pattern and move them

查看:17
本文介绍了查找名称匹配模式的目录并移动它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆目录,例如 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 并引用替换符号 {} 使您的脚本更加健壮.它将处理出现不可打印字符的大多数情况.这基本上是说它使用 字符分隔符而不是 来传递行.

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 char delimiter instead of a .

这篇关于查找名称匹配模式的目录并移动它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆