Introduction
In this project, you will learn how to copy files from the /etc directory with a last modified year in 2022 to the /tmp/etc directory while preserving the directory structure.
🎯 Tasks
In this project, you will learn:
- How to create a script to automate the file copying process
- How to use the
findcommand to search for files based on their last modified date - How to use the
cpcommand to copy files while preserving the directory structure
🏆 Achievements
After completing this project, you will be able to:
- Automate the process of copying files from a specific directory based on their last modified date
- Understand how to use common shell commands like
findandcpto manipulate files and directories - Apply your knowledge to solve real-world file management tasks
Create the Collect Files Script
In this step, you will create a script to copy files from the /etc directory with a last modified year in 2022 to the /tmp/etc directory while preserving the directory structure.
- Open a text editor and create a new file named
collect_files.shin the/home/labex/projectdirectory. - Add the following code to the
collect_files.shfile:
#!/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."
This script uses the find command to search for files in the /etc directory that were last modified in the year 2022. The -newermt and ! -newermt options are used to filter the files based on their last modified date. The cp command is then used to copy the files to the /tmp/etc directory, preserving the directory structure.
- Save the file.
Test the Script
In this step, you will test the collect_files.sh script to ensure it is working as expected.
- Open a terminal and navigate to the
/home/labex/projectdirectory. - Run the script with the following command:
sudo sh collect_files.sh
This command runs the script with elevated privileges, which is necessary to copy files from the /etc directory.
- After the script finishes running, check the
/tmp/etcdirectory to ensure that the files with a last modified year of 2022 have been copied correctly. - Verify that the directory structure has been preserved. For example, if the
/etc/fstabfile was copied, you should see the file at the path/tmp/etc/fstab.
If the script is working as expected, you have completed the project. If you encounter any issues, review the script and the instructions, and make any necessary adjustments.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



