Frage

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?

War es hilfreich?

Lösung

Use extended globbing:

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

Andere Tipps

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top