一个文件粘贴到具有文件夹名称的多个文件夹中

一个文件粘贴到具有文件夹名称的多个文件夹中

我有一个报告文件(1.xls)这个1.xls应该复制到所有名称文件夹c / desk/test/在测试文件夹中我有这个文件1.xls我还有文件夹aaa_001 bbb_002 ccc_003我需要将1.xls粘贴到所有文件夹中并且1.xls应该根据文件夹名称重命名。我想要这样:c/desktop/test/aaa_001/aaa_001.xls c/desktop/test/aaa_001/bbb_002.xls c/desktop/test/aaa_001/ccc_003.xls请帮帮我

答案1

您可以使用 bash 脚本来执行所需的操作。如果您希望文件自动更新,可能还值得考虑使用链接。请注意,如果是软链接,某些存档格式会将链接打包为链接,而不是实际文件。

#!/bin/bash
# put all arguments into the args array
args=("$@")
# get your single source file from argument one
src=${args[0]}

# loop over the arguments array, each value is put into $p
idx=0
for dst in ${args[@]}
do
  # skip the first argument
  if [ ${idx} -gt 0 ]
  then
    # copy file
    cp ${src} ${dst}
  fi
  idx=$((${idx} + 1))
done

然后,您只需使用第一个参数作为单一源并将所有后续参数作为目标来调用脚本

或者你可以使用xargs

# delimited by new lines `\n`
echo '/path/to/dir1\n/path/to/dir2' | xargs -I{} cp 'file/to/copy' {}

相关内容