Question

I'm copying all subdirectories with its contents to my current directory as follows:

cp -r dirToCopy/* .

But in the folder dirToCopy, there is one subfolder called dirNotToCopy which should not be copied.

How can I filter that particular folder out of my expression?

Was it helpful?

Solution

Use extended globbing:

shopt -s extglob
cp -r dirToCopy/!(dirNotToCopy) .

OTHER TIPS

Well if you want to do it in single line:

find /path_to/dirToCopy -mindepth 1 -type d  ! -name dirNotToCopy -exec cp -r {} . \; 

One more way of doing the same.

find /path_to/dirToCopy -maxdepth 1 -type d  ! -name dirNotToCopy -exec cp -r {} . \; 

Instead of using mindepth suggested in the other answer, we should use maxdepth. (I can't comment or edit another answer since I do not have enough reputation yet)

Also note that this command only copies the subdirectories, not the files in the directory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top