linuxbashterminalcommandcp

How to use 'cp' command to exclude a specific directory?


I want to copy all files in a directory except some files in a specific sub-directory. I have noticed that cp command didn't have the --exclude option. So, how can I achieve this?


Solution

  • rsync is fast and easy:

    rsync -av --progress sourcefolder /destinationfolder --exclude thefoldertoexclude
    

    You can use --exclude multiples times.

    rsync -av --progress sourcefolder /destinationfolder --exclude thefoldertoexclude --exclude anotherfoldertoexclude
    

    Note that the dir thefoldertoexclude after --exclude option is relative to the sourcefolder, i.e., sourcefolder/thefoldertoexclude.

    Also you can add -n for dry run to see what will be copied before performing real operation, and if everything is ok, remove -n from command line.