これは、シェル スクリプトの魔法を使えば簡単です。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 シェル スクリプトの詳細については、さまざまな書籍やインターネット サイトを参照してください。Advanced Bash-Scripting Guideから始めることができます。