使用 find 查找目录并将其移动到不同的路径

乌默尔

我在数百个目录中有数十万个文件。

一个示例目录结构是

./main/foo1/bar/*
./main/foo2/bar/*
./main/foo3/bar/*
./main/foo1/ran/*
./main/foo2/ran/*

对于具有“bar”目录的文件夹,我想将内容移动到以下结构。

./secondary/bar/foo1/*
./secondary/bar/foo2/*
./secondary/bar/foo3/*

这可以使用 find 和 mv 来完成吗?

pLumo

一些小脚本来实现这一点:

# Set the variables
main="./main"
secondary="./secondary"
foo_depth=3
bar_depth=4
bar_name="bar"

# let * match hidden files
shopt -s dotglob

# Loop through find (which is declared after done)
while IFS= read -r dir; do

    # Read names of foo dir
    foo=$(printf '%s' "$dir" | cut -d'/' -f $foo_depth)

    # mkdir target and mv dir there
    target="${secondary}/${bar_name}/${foo}"
    mkdir -p "$target"
    mv "$dir"/* "$target"

    # as you mv the content of bar dir only,
    # you may want to remove the full path to that folder
    # rmdir -p will do that without deleting anything that is not empty,
    # we can ignore the "failed to remove" messages.
    [ $? = 0 ] && rmdir -p "${dir}"

done < <(find "$main" -type d -name "$bar_name")

# Turn off dotglob
shopt -u dotglob

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用find命令检查目录大小?

Linux Bash-使用find排除目录

如何获取我使用Intent选择的目录的路径并将其用作文件路径?

如何使用请求从Json链接收集数据并将其移动到Pandas Dataframe

如何保存文件并将其移动到所需位置,然后再下载(python,selenium)?使用Chrome WebDriver

使用.Find查找文本

使用find时如何排除目录

使用find + xargs移动文件:目标不是目录?

查找所有.mp3并使用批处理移动到指定目录

查找比在目录中创建其父文件夹更早的文件并将其移动到该目录中

使用find移动文件时保留目录结构

使用Vim剪切和粘贴元素并将其有效地移动到另一行

如何在Linux中使用bash压缩90天的旧文件并将其移动到特定文件夹

如何查找特定文件并将其移动到特定目录?

如何使用“ stat”检测新文件,然后将其移动到其他目录?

使用File :: Find的子目录的perl列表路径

使用python查找PDF和DOC文件并将其移动到其他目录

如何使用XMonad打开应用程序并将其移动到特定的窗口堆栈位置?

使用`find`排除多个路径匹配模式

压缩文件并将其移动到相应目录

使用名称对文件进行分组,然后使用awk将其移动到相应目录

查找包含字符串的文件,并将其对应的文件移动到其他目录

使用 find 查找文件

使用 find 递归移动文件夹

用于在文件中查找文件名并将其移动到不同目录的 Powershell 脚本

在 Dockerfile 中编译文件并将其移动到不同的目录 (Docker)

使用变量定位一个子数组并将其移动到主数组的顶部

在 find 命令中使用文件而不是目录

如何使用赛普拉斯删除“选定”属性并将其移动到另一个选项?