我有一个文件夹,其中包含许多带有子文件夹 (/...) 的文件夹,其结构如下:
_30_photos/combined
_30_photos/singles
_47_foo.bar
_47_foo.bar/combined
_47_foo.bar/singles
_50_foobar
使用命令将显示find . -type d -print | grep '_[0-9]*_'结构为** 的所有文件夹。但是我生成了一个仅捕获 */combined 文件夹的正则表达式:
_[0-9]*_[a-z.]+/combined但是当我将它插入到 find 命令中时,不会打印任何内容。
下一步是为每个组合文件夹(在我的硬盘上的某处)创建一个文件夹,并将组合文件夹的内容复制到新文件夹中。新文件夹名称应与子文件夹的父名称相同,例如 _47_foo.bar。搜索后可以使用 xargs 命令来实现吗?
你不需要 grep:
find . -type d -regex ".*_[0-9]*_.*/combined"
对于其余的:
find . -type d -regex "^\./.*_[0-9]*_.*/combined" | \
   sed 's!\./\(.*\)/combined$!& /somewhere/\1!'   | \
   xargs -n2 cp -r
使用 basicgrep你需要逃避+:
... | grep '_[0-9]*_[a-z.]\+/combined'
或者您可以使用“扩展正则表达式”版本(egrep或grep -E[感谢 chepner]),其中+不必转义。
xargs可能不是进行上面描述的复制的最灵活的方法,因为与多个命令一起使用很棘手。您可能会发现 while 循环具有更大的灵活性:
... | grep '_[0-9]*_[a-z.]\+/combined' | while read combined_dir; do 
    mkdir some_new_dir
    cp -r ${combined_dir} some_new_dir/
done
如果您想要一种自动命名 .bashrc 的名称的方法,请查看bash 字符串操作some_new_dir。