How to copy files and folders in the linux console excluding some of them by regular expression

    Hello, harazhiteli.

    Do not judge strictly, I am new to linux and this is my first post on the hub, but maybe it will be useful to someone similar to me.

    Faced with the need to solve the problem described in the header. I did not find a ready-made solution. I wrote a sh script (cpexclude.sh) that first copies everything and then removes the excess:

    #!/bin/bash
    if [ $# -lt 3 ] ; then
    	echo "cpexclude usage: pathFrom pathTo excludeRegex"
    	exit 0
    fi
    pathFrom=$1
    pathTo=$2
    excludeRegex=$3
    # Copy everything
    echo `cp -a $pathFrom/. $pathTo`
    # Delete by excludeRegex
    echo `find $pathTo -regex $excludeRegex -delete`
    

    Added it to ~ / .bashrc

    alias cpexclude='/path/to/cpexclude.sh'
    

    I use it from time to time.
    If someone has a more acceptable solution on their mind, I beg you.

    Upd. I did not fully describe the task. There is still a need for the structure of the subdirectories of the copied directory to be preserved and for the subdirectories themselves to be copied, and not just the files in them.

    Upd2. In the comments prompted:
    • rsync -r --exclude=PATTERN from/ to/
      

    • SRC=~/work/soruce; DEST=~/work/test; REGEXP=js; pushd $SRC; for I in $(find ./ | grep -v "$REGEXP"); do if [ -d $I ]; then mkdir $DEST/$I; else cp $I $DEST/$I; fi; done; popd 2>/dev/null
      

    Also popular now: