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

Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL shell(("`Shell`")) -.-> shell/ControlFlowGroup(["`Control Flow`"]) 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/ControlFlowGroup -.-> shell/if_else("`If-Else Statements`") 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/FileandDirectoryManagementGroup -.-> linux/locate("`File Locating`") 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`") subgraph Lab Skills shell/if_else -.-> lab-301465{{"`Copy Large Files with Preserved Structure`"}} linux/source -.-> lab-301465{{"`Copy Large Files with Preserved Structure`"}} linux/echo -.-> lab-301465{{"`Copy Large Files with Preserved Structure`"}} linux/mkdir -.-> lab-301465{{"`Copy Large Files with Preserved Structure`"}} linux/find -.-> lab-301465{{"`Copy Large Files with Preserved Structure`"}} linux/locate -.-> lab-301465{{"`Copy Large Files with Preserved Structure`"}} linux/cp -.-> lab-301465{{"`Copy Large Files with Preserved Structure`"}} shell/shebang -.-> lab-301465{{"`Copy Large Files with Preserved Structure`"}} shell/comments -.-> lab-301465{{"`Copy Large Files with Preserved Structure`"}} shell/quoting -.-> lab-301465{{"`Copy Large Files with Preserved Structure`"}} shell/variables_decl -.-> lab-301465{{"`Copy Large Files with Preserved Structure`"}} shell/variables_usage -.-> lab-301465{{"`Copy Large Files with Preserved Structure`"}} end

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.

Summary

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

Other Linux Tutorials you may like