Introduction
In this project, you will learn how to copy files larger than 10K from the /etc directory to the /tmp/etc directory, preserving the directory structure. This is a useful skill for managing and organizing files on a Linux system.
🎯 Tasks
In this project, you will learn:
- How to create a script to copy files based on size
- How to use the
findcommand to locate files that meet specific criteria - How to preserve the directory structure when copying files
- How to run a script with elevated privileges using
sudo
🏆 Achievements
After completing this project, you will be able to:
- Automate the process of copying files based on size
- Maintain the directory structure when copying files
- Use the
findcommand effectively to locate files that meet specific criteria - Run scripts with elevated privileges to access protected files and directories
Create the Copy Script
In this step, you will create the script that will copy the files larger than 10K from the /etc directory to the /tmp/etc directory.
- Open the
copy.shfile in the/home/labex/projectdirectory using a text editor. - Add the following code to the file:
#!/bin/zsh
## This script copies files larger than 10K from a source directory to a target directory.
## Define the source directory and target directory
source_dir="/etc"
target_dir="/tmp"
## Create the target directory if it doesn't exist
mkdir -p "$target_dir"
## Use the find command to locate files larger than 10K and copy them to the target directory
find "$source_dir" -type f -size +10k -exec cp --parents --dereference "{}" "$target_dir" \;
echo "File copying complete."
- Save the file.
In this step, you have created the copy.sh script that will copy the files larger than 10K from the /etc directory to the /tmp/etc directory, preserving the directory structure.
Run the Copy Script
In this step, you will run the copy.sh script to copy the files.
- Open a terminal and navigate to the
/home/labex/projectdirectory. - Run the script using the following command:
sudo sh copy.sh
This will run the script with elevated privileges, allowing it to copy files that the labex user may not have access to.
- Wait for the script to complete. You should see the message "File copying complete." when the script has finished.
In this step, you have run the copy.sh script to copy the files larger than 10K from the /etc directory to the /tmp/etc directory.
Verify the Copied Files
In this step, you will verify that the files have been copied correctly.
- Open a file manager and navigate to the
/tmp/etcdirectory. - Observe the directory structure and the files that have been copied. You should see that the directory structure has been preserved, and only the files larger than 10K have been copied.
- You can also use the
ls -lcommand in the terminal to list the files and their sizes in the/tmp/etcdirectory.
In this step, you have verified that the files have been copied correctly from the /etc directory to the /tmp/etc directory.
Congratulations! You have successfully completed the project of copying files larger than 10K from the /etc directory to the /tmp/etc directory, preserving the directory structure.
Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



