简体   繁体   English

查找名称与pattern匹配的目录并移动它们

[英]Find directories with names matching pattern and move them

I have a bunch of directories like 001/ 002/ 003/ mixed in with others that have letters in their names. 我有一个像一堆目录001/ 002/ 003/混合与其他人有他们的名字字母。 I just want to grab all the directories with numeric names and move them into another directory. 我只是想用数字名称获取所有目录并将它们移动到另一个目录中。

I try this: 我试试这个:

file */ | grep ^[0-9]*/ | xargs -I{} mv {} newdir

The matching part works, but it ends up moving everything to the newdir ... 匹配部分工作,但最终将所有内容移动到newdir ...

I am not sure I understood correctly but here is at least something to help. 我不确定我是否正确理解,但这里至少有一些帮助。

Use a combination of find and xargs to manipulate lists of files. 使用find和xargs的组合来操作文件列表。

find -maxdepth 1 -regex './[0-9]*' -print0 | xargs -0 -I'{}' mv "{}" "newdir/{}"

Using -print0 and -0 and quoting the replacement symbol {} make your script more robust. 使用-print0-0并引用替换符号{}可使脚本更加健壮。 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 . 这基本上说它使用\\0字符分隔符而不是\\n来传递行。

mv is not powerfull enough by itself. mv本身并不够强大。 It cannot work on patterns. 它不能用于模式。

Try this approach: Rename multiple files by replacing a particular pattern in the filenames using a shell script 尝试此方法: 通过使用shell脚本替换文件名中的特定模式来重命名多个文件

Either use a loop or a rename command. 使用循环或rename命令。

With loop and array, 使用循环和数组,

Your script would be something like this: 你的脚本将是这样的:

#!/bin/bash
DIR=( $(file */ | grep ^[0-9]*/ | awk -F/ '{print $1}') ) 

for dir in "${DIR[@]}"; do
   mv $dir /path/to/DIRECTORY
done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM