ファイル収集スクリプトを作成する
このステップでは、2022 年の最終更新日付を持つ /etc
ディレクトリからのファイルをディレクトリ構造を維持したまま /tmp/etc
ディレクトリにコピーするためのスクリプトを作成します。
- テキストエディタを開き、
/home/labex/project
ディレクトリに collect_files.sh
という名前の新しいファイルを作成します。
collect_files.sh
ファイルに以下のコードを追加します。
#!/bin/zsh
## Script: collect_files.sh
## Description: Copies files from the /etc directory with a last modified year in 2022 to the /tmp/etc directory while preserving directory structure.
source_dir="/etc"
target_dir="/tmp"
year="2022"
## Create the target directory
mkdir -p "$target_dir"
## Use the find command to search for files in the source directory with a last modified year in 2022 and copy them to the target directory
find "$source_dir" -type f -newermt "$year-01-01"! -newermt "$year-12-31" -exec cp --parents --dereference "{}" "$target_dir" \;
echo "File copying completed."
このスクリプトは、find
コマンドを使用して、2022 年に最終更新された /etc
ディレクトリ内のファイルを検索します。-newermt
と ! -newermt
オプションは、最終更新日付に基づいてファイルをフィルタリングするために使用されます。その後、cp
コマンドを使用して、ディレクトリ構造を維持したまま /tmp/etc
ディレクトリにファイルをコピーします。
- ファイルを保存します。