cp 数百个文件到不同的目录



标题不是最好的,很抱歉,我对脚本很陌生。

我正在尝试从每个目录中复制 2 个文件,并将这些化合物放在仅共享目录名称的单独目录中。

为了清楚起见;

/path/to/directory/all/variable_directories/

此目录中将有多个文件,我需要 2 个文件,这些文件在每个单独的变量目录中具有相同的名称。

我正在尝试从每个单独的变量目录中复制这两个文件,并根据/variable_directory/的基本名称将它们放入变量目录中

复制目的地将是;

/path/to/magical/shit/subset/set_with_variable_name/variable_directories/

每个/set_with_variable_name/中只有一些目标目录

脚本需要能够遍历每个/set_with_variable_name/,直到找到共享这些文件最初从中执行 cp's 的目录的基名的目录

大约有 100 个目录 到 CP 从和到和总共大约 200 个文件需要适当地复制和排序。

我可以使用它将所有文件cp到相同的目录;

#! /usr/bin/env bash
for i in */;
do cd $i;
cp filename /path/to/destination/;
cp other_filename /path/to/destination/;
cd ..;
done;

这是将文件分类到我完全迷失的正确目的地。

我感谢任何帮助,我是这种类型的脚本的新手

看起来你需要这样的东西。

# looping through all the variable directories
for var_dir in <path1>/all/*; do
# creating the destination directory if not exists
mkdir -p "<path2>/set_with_variable_name/$(basename ${var_dir})"
# copying the first file
cp "${var_dir}/filename1" "<path2>/set_with_variable_name/$(basename ${var_dir})/filename1"
# and the second
cp "${var_dir}/filename2" "<path2>/set_with_variable_name/$(basename ${var_dir})/filename2"
done

最新更新