Redline1111
Redline1111

Reputation: 131

Avoid copying subdirectory contents multiple times when recursively copying files

The script does a simple operation of copying all files (excluding ".log" files) and all sub-directories into destination folder. The issue I am running into is the files that reside in sub-directories are also being copied into the destination folder so the end result is that those files show up twice; in the destination folder and also the sub folders of that folder. How can this be avoided? Thanks

source_dir="path/to/source"
dest_dir="path/to/destination"

arg=${1}
echo $arg

if [[ ! -d $dest_dir ]]; then
    echo "creating destination directory $dest_dir"
    mkdir $dest_dir
fi  

#copy all files that don't end with .log
for resource in `find $source_dir ! -name '*.log'`; do
        echo "copying resource $resource..."
        cp -r $resource $dest_dir
done

Upvotes: 0

Views: 174

Answers (2)

jthill
jthill

Reputation: 60275

I don't think rsync has a recursion limiter, and for something like this it's probably overkill anyway.

(cd /path/to/source; find -maxdepth 1 ! -name \*.log | cpio -pdv /path/to/destination)

Upvotes: 1

WEBjuju
WEBjuju

Reputation: 6581

Consider replacing the for case with a single command:

rsync -avz --exclude '*.log' path/to/source/ path/to/destination/

rsync does not require rtools to be running. and it works great locally, for a purpose just like this.

Upvotes: 3

Related Questions