Skip to content

A Shell Script to Copy All files with Same Structure

A Shell Script to Copy All files from source directory to destination directory with the same structure. Copy files between two directory with same path.

On this page

This shell script is designed for a specific task required for our server. So sharing the script with Tecadmin readers. It might be helpful for you.

Requirement

Our requirement is to copy all files created in directory to other directory with same directory structure. Script will run on a regular interval, search for all files created in source directory and copy them to destination directory. The script must maintain the same directory structure on destination directory. After that change the permission on destination directory. After successful copy of file, script must remove the file from source directory.

Shell Script

Create a shell script on your system and add below content. Update source and destination directories to proper location.

Set CHANGE_OWNERSHIP to 0, if you don’t want to change ownership on destination files.

Save your file and close it.

Then set the execute permission to the script file.

chmod +x script.sh

Rahul

Rahul Kumar is the founder and chief editor of TecAdmin.net. A Red Hat Certified Engineer (RHCE), he has worked in IT since 2009, with hands-on experience in Linux system administration, DevOps, web hosting, cloud platforms and automation. He started TecAdmin in 2013 to share practical, step-by-step guides, and leads its editorial work with one aim: making technical learning clear and useful for sysadmins, developers and beginners alike.

1 Comment

  1. Nice to see shell script.
    Using cp -R SOURCE_DIR DEST_DIR
    will do same operation to copy files from source to destination retains directory structure?

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top