借助 shell 脚本的魔力,这很容易。我假设你有 bash 可用。在包含这些子目录的目录中创建一个新文件;将其命名为copy_logs.sh
. 将以下文本复制粘贴到其中:
#!/bin/bash
# copy_logs.sh
# Copies all files named log.lammps from all subdirectories of this
# directory, except logs/, into subdirectory logs/, while appending the name
# of the originating directory. For example, if this directory includes
# subdirectories 1/, 2/, foo/, and logs/, and each of those directories
# (except for logs/) contains a file named log.lammps, then after the
# execution of this script, the new file log.lammps.1, log.lammps.2, and
# log.lammps.foo will have been added to logs/. NOTE: any existing files
# with those names in will be overwritten.
DIRNAMES=$( find . -type d | grep -v logs | sed 's/\.//g' | sed 's/\///g' | sort )
for dirname in $( echo $DIRNAMES )
do
cp -f $dirname/foo.txt logs/foo$dirname
echo "Copied file $dirname/foo.txt to logs/foo.$dirname"
done
请参阅脚本的注释了解它的作用。保存文件后,您需要通过chmod a+x copy_logs.sh
命令行命令使其可执行。在此之后,您可以通过在命令行上键入来执行它,./copy_logs.sh
而您的工作目录是包含脚本和子目录的目录。如果将该目录添加到 $PATH 变量中,则copy_logs.sh
无论您的工作目录是什么,您都可以进行命令。
(我用 GNU bash v4.2.24 测试了脚本,所以它应该可以工作。)
有关 bash shell 脚本的更多信息,请参阅任意数量的书籍或互联网站点;您可以从Advanced Bash-Scripting Guide开始。