파일 수집 스크립트 생성
이 단계에서는 마지막 수정 연도가 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 디렉토리로 복사합니다.
- 파일을 저장합니다.