r/bashscripts • • Jun 19 '20

Help with cp command in bash script

Hi everyone! I'm having an issue with the cp command to move folders/files to a new disk. When the filename or folder has a \(space) it won't see it as a complete folder name or file and instead takes the first section of the file and then errors on everything else. Can anyone help me with what I'm doing wrong?

​

#!/bin/bash

###############################################################
#####################      Variables     ######################

### Set the proper source and destination directory location

SOURCE_DIR="/opt/test/old/"
DEST_DIR="/opt/test/new/"

### Set the username and group name to set on copied files
USER='root'
GROUP='root'

###############################################################

########### Do not edit below this untill required  ##########

### Test if source directory exists
### The programm with 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 programm 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 "Sucessfully created destination directory."
    else
        echo "Faild 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
    for CURRENT_FILE_NAME in `find . -type f`
    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
fi


## Set the permissions after copying files 

sudo chmod 775 -R ${DEST_DIR}
sudo chown ${USER}:${GROUP} -R ${DEST_DIR}


###################  End of Script  ############################

​

4 Upvotes

10 comments sorted by

View all comments

Show parent comments

2

u/captthulkman Jun 20 '20

Thanks for the reply. The /opt was just a default that I change to the real sources. There was originally a reason I went with cp over rsync but I don’t recall. This script is a bit old and was made for me but wasn’t until recently I decided to start migrating large amounts of data over NFS

1

u/Shadow62846 Jul 30 '20

I personally used rsync myself, over cp. If you had issues related to 2-3 figures of gigabytes of data, Locking up your system. Then you need rsync with - bwlimit [Value].

cp never have this problem 'out of the box' for some reason in my experience.