Facebook X (Twitter) Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook X (Twitter) Instagram
    TecAdmin
    You are at:Home»Programming»Bash Shell»A Shell Script to Copy All files with Same Structure

    A Shell Script to Copy All files with Same Structure

    By RahulJuly 10, 20201 Min Read

    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.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    #!/bin/bash
     
    #####################      Variables     #############################
     
    ### Set the proper source and destination directory location
     
    SOURCE_DIR="/source/dir/"
    DEST_DIR="/desination/dir/"
    TMP_FILE=/tmp/copyfileslist.txt
     
    ### Set the username and group name to set permission on copied files
    ### Set CHANGE_OWNERSHIP to 1 to change ownership or 0 to unchanged it
     
    CHANGE_OWNERSHIP=1
    USER='root'
    GROUP='root'
     
     
    ########### Do not edit below this until required  #################
     
    ### Test if source directory exists
    ### The script will stop if source not exists
     
    if [ -d "${SOURCE_DIR}" ]; then
            echo "Source directory found"
    else
            echo "Source directory not found. Please check above variables are set correctly"
            echo "script exited"
            exit 1
    fi
     
    ### Test if destination directory exists
    ### The script will create destination directory if not exists.
    ### If failed to create directory, the script will terminate
     
    if [ -d "${DEST_DIR}" ]; then
            echo "Destination directory found, all ok"
    else
            echo "Destination directory not found, creating now"
            mkdir -p "${DEST_DIR}"
            if [ $? -eq 0 ]; then
                    echo "Successfully created destination directory."
            else
                    echo "Failed to create destination directory. Script exited"
                    exit 1
            fi
    fi
     
     
    ### Copy all files available on source directory
    ### After successfully copying file remove it from source directory.
     
    cd "${SOURCE_DIR}"
     
    if [ $? -eq 0 ]; then
            find . -type f > ${TMP_FILE}
     
            while read CURRENT_FILE_NAME
            do
                    cp --parents "${CURRENT_FILE_NAME}" "${DEST_DIR}"
                    if [ $? -eq 0 ]; then
                            echo "File ${CURRENT_FILE_NAME} successfully copied."
                            rm -f "${CURRENT_FILE_NAME}"
                    else
                            echo "File ${CURRENT_FILE_NAME} failed to copy"
                    fi
            done < ${TMP_FILE}
            rm -f ${TMP_FILE}
    fi
     
     
    ## Set the permissions after copying files
     
    if [ ${CHANGE_OWNERSHIP} -eq 1 ]; then
    sudo chmod 775 -R "${DEST_DIR}"
    sudo chown ${USER}:${GROUP} -R "${DEST_DIR}"
    fi
     
    ###################  End of Script  ###################################

    Save your file and close it.

    Then set the execute permission to the script file.

    chmod +x script.sh
    

    bash shell script
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    An Introduction to Bash Variables

    An Introduction to Bash Variables

    Bash LOCAL and GLOBAL Variables

    Bash LOCAL and GLOBAL Variables

    Bash Script to Reverse a Number

    View 1 Comment

    1 Comment

    1. Vishwas on July 30, 2020 9:44 am

      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?

      Reply

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Difference Between Full Virtualization vs Paravirtualization
    • Virtualization vs. Containerization: A Comparative Analysis
    • Using .env Files in Django
    • Using .env File in FastAPI
    • Setting Up Email Notifications for Django Error Reporting
    Facebook X (Twitter) Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.