[英]how to create subfolders and move files
I have a folder called p/ and into p/ I have several subfolders, like this:我有一个名为 p/ 的文件夹,进入 p/ 我有几个子文件夹,如下所示:
$[~/p] ls
a/ b/ c/
a/ b/ and c/ also have multiple subfolders each. a/ b/ 和 c/ 每个也有多个子文件夹。 I'm trying to find a file that match a specific pattern into each folder and subfolder of p/ and move them to a new directory into its corresponding a/, b/ and c/ folder.我正在尝试在 p/ 的每个文件夹和子文件夹中找到与特定模式匹配的文件,并将它们移动到新目录中对应的 a/、b/ 和 c/ 文件夹中。 So, a/ would have a new subfolder called x/ and into x/ will be moved all the matched files found in a/, b/ would have a new subfolder called x/ and into x/ will be moved all the matched files found in b/ and so on.因此,a/ 将有一个名为 x/ 的新子文件夹,将在 a/ 中找到的所有匹配文件移动到 x/ 中,b/ 将有一个名为 x/ 的新子文件夹,并将所有匹配文件移动到 x/ 中在 b/ 等等。
I have tried:我努力了:
pth=path/to/p
for dir in ${pth}/*; do
mkdir -- "$dir/x";
find . -name '*match*' -exec mv -t ./x '{}' +;
done
However it's not working, it makes the x/ subfolder into a/, b/ and c/ but it's not moving anything.但是它不起作用,它使 x/ 子文件夹变成 a/、b/ 和 c/,但它没有移动任何东西。
I got:我有:
mv: failed to access to './x': No such file or directory mv:无法访问“./x”:没有这样的文件或目录
What I'm doing wrong??我做错了什么?? Could you help me please?请问你能帮帮我吗?
Edit: This is an example of the structure of the folders:编辑:这是文件夹结构的示例:
p/
-a/
--t/
--q/
--p/
-- files-to-match
--f/
--qd/
--pe/
-- files-to-match
--d/
--qu/
--ip/
-- files-to-match
Same for folder b and c and the rest of folders that I have.文件夹 b 和 c 以及我拥有的其余文件夹也是如此。 The name of subfolders are not always the same.子文件夹的名称并不总是相同的。
The code that @ikegami provide me is working, but the location of the folder x/ ends at the same level of "files-to-match" but I'd like it to be located at the first level, that is at the same level t/, f/ and d/ (for this example). @ikegami 为我提供的代码正在运行,但文件夹 x/ 的位置在“匹配文件”的同一级别结束,但我希望它位于第一级别,即同一级别级别 t/、f/ 和 d/(对于此示例)。
So the final structure would be:所以最终的结构是:
p/
-a/
--t/
--q/
--p/
--
--f/
--qd/
--pe/
--
--d/
--qu/
--ip/
--
--x/
--matched-files
You never created ./x
, nor is that the directory into which you want to move the file anyway.您从未创建./x
,也不是您想要将文件移动到的目录。
Also, if you move the file before find
finishes, it could repeatedly find the same file.此外,如果您在find
完成之前移动文件,它可能会重复找到相同的文件。
# $pth can't start with `-`.
# Prefix with `./` if necessary.
# $qfn and $dir_qfn won't start with `-`.
tmp_qfn="$( mktemp )"
find "$pth" -name '...' -type f -print >"$tmp_qfn"
while IFS= read -r qfn; do
dir_qfn="$( dirname "$qfn" )"/x
mkdir "$dir_qfn"
mv "$qfn" "$dir_qfn"
done <"$tmp_qfn"
rm -- "$tmp_qfn"
You're not using the right directory.您没有使用正确的目录。 Change改变
find . -name '*match*' -exec mv -t ./x '{}' +;
to到
find "$dir" -name '*match*' -exec mv -t "$dir/x" '{}' +;
Also, you need to avoid moving files from x
by excluding that path.此外,您需要通过排除该路径来避免从x
移动文件。
# $pth can't start with `-`.
# Prefix with `./` if necessary.
# $dir won't start with `-`.
pth=path/to/p
for dir in "$pth"/*; do
mkdir "$dir/x";
find "$dir" \
-wholename "$dir/x" -prune \
-or \
-name '*match*' -exec mv -t "$dir/x" {} +
done
Test测试
mkdir -p p/a/t/q/p
touch p/a/t/q/p/abc.json
mkdir -p p/a/f/qd/pe
touch p/a/t/q/p/def.json
mkdir -p p/b/t/q/p
touch p/b/t/q/p/ghi.json
pth=p
find "$dir" -name '*.json'
printf -- '--\n'
for dir in "$pth"/*; do
mkdir "$dir/x";
find "$dir" \
-wholename "$dir/x" -prune \
-or \
-name '*.json' -exec mv -t "$dir/x" {} +
done
find "$dir" -name '*.json'
Output输出
p/a/t/q/p/def.json
p/a/t/q/p/abc.json
p/b/t/q/p/ghi.json
--
p/a/x/def.json
p/a/x/abc.json
p/b/x/ghi.json
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.