Collect Files From Specified Time

LinuxLinuxBeginner
Practice Now

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 find command to search for files based on their last modified date
  • How to use the cp command 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 find and cp to 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.

  1. Open a text editor and create a new file named collect_files.sh in the /home/labex/project directory.
  2. Add the following code to the collect_files.sh file:
#!/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.

  1. Save the file.

Test the Script

In this step, you will test the collect_files.sh script to ensure it is working as expected.

  1. Open a terminal and navigate to the /home/labex/project directory.
  2. 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.

  1. After the script finishes running, check the /tmp/etc directory to ensure that the files with a last modified year of 2022 have been copied correctly.
  2. Verify that the directory structure has been preserved. For example, if the /etc/fstab file 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.

âœĻ Check Solution and Practice

Summary

Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.

Other Linux Tutorials you may like