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

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux(("`Linux`")) -.-> linux/FileandDirectoryManagementGroup(["`File and Directory Management`"]) linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) shell(("`Shell`")) -.-> shell/BasicSyntaxandStructureGroup(["`Basic Syntax and Structure`"]) shell(("`Shell`")) -.-> shell/VariableHandlingGroup(["`Variable Handling`"]) shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) shell(("`Shell`")) -.-> shell/AdvancedScriptingConceptsGroup(["`Advanced Scripting Concepts`"]) linux/BasicSystemCommandsGroup -.-> linux/source("`Script Executing`") linux/BasicSystemCommandsGroup -.-> linux/echo("`Text Display`") linux/FileandDirectoryManagementGroup -.-> linux/mkdir("`Directory Creating`") linux/FileandDirectoryManagementGroup -.-> linux/find("`File Searching`") linux/BasicFileOperationsGroup -.-> linux/cp("`File Copying`") shell/BasicSyntaxandStructureGroup -.-> shell/shebang("`Shebang`") shell/BasicSyntaxandStructureGroup -.-> shell/comments("`Comments`") shell/BasicSyntaxandStructureGroup -.-> shell/quoting("`Quoting Mechanisms`") shell/VariableHandlingGroup -.-> shell/variables_decl("`Variable Declaration`") shell/VariableHandlingGroup -.-> shell/variables_usage("`Variable Usage`") shell/ControlFlowGroup -.-> shell/for_loops("`For Loops`") shell/ControlFlowGroup -.-> shell/while_loops("`While Loops`") shell/AdvancedScriptingConceptsGroup -.-> shell/arith_ops("`Arithmetic Operations`") subgraph Lab Skills linux/source -.-> lab-301463{{"`Collect Files From Specified Time`"}} linux/echo -.-> lab-301463{{"`Collect Files From Specified Time`"}} linux/mkdir -.-> lab-301463{{"`Collect Files From Specified Time`"}} linux/find -.-> lab-301463{{"`Collect Files From Specified Time`"}} linux/cp -.-> lab-301463{{"`Collect Files From Specified Time`"}} shell/shebang -.-> lab-301463{{"`Collect Files From Specified Time`"}} shell/comments -.-> lab-301463{{"`Collect Files From Specified Time`"}} shell/quoting -.-> lab-301463{{"`Collect Files From Specified Time`"}} shell/variables_decl -.-> lab-301463{{"`Collect Files From Specified Time`"}} shell/variables_usage -.-> lab-301463{{"`Collect Files From Specified Time`"}} shell/for_loops -.-> lab-301463{{"`Collect Files From Specified Time`"}} shell/while_loops -.-> lab-301463{{"`Collect Files From Specified Time`"}} shell/arith_ops -.-> lab-301463{{"`Collect Files From Specified Time`"}} end

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.

Summary

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

Other Linux Tutorials you may like