Copy Large Files with Preserved Structure

LinuxLinuxBeginner
Practice Now

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 find command 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 find command 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.

  1. Open the copy.sh file in the /home/labex/project directory using a text editor.
  2. 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."
  1. 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.

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

  1. 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.

  1. Open a file manager and navigate to the /tmp/etc directory.
  2. 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.
  3. You can also use the ls -l command in the terminal to list the files and their sizes in the /tmp/etc directory.

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.

âœĻ 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