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
1 Comment
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?