這很容易與貝殼腳本的魔力peasy。我假設你有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開始。
你願意接受這一答案之一,如果你的作品? –