How to copy all files within a directory with globstar?

3.9k views Asked by At

Say I want to copy all files within dir to dest:

$ tree .
.
├── dest
└── dir
    ├── dir
    │   ├── file1
    │   └── file2
    └── file3

This is easy if I know the filenames and the directory depths:

$ echo dir/f* dir/*/*
dir/file3 dir/dir/file1 dir/dir/file2

$ cp dir/f* dir/*/* dest/

$ tree dest/
dest/
├── file1
├── file2
└── file3

It's also easy (with globstar) to get only the directories:

$ echo dir/**/*/
dir/dir/

But I don't know how to glob only the files, e.g. the following doesn't work:

$ echo dir/**/*!(/)
dir/**/*!(/)
1

There are 1 answers

4
anubhava On

One option is to use find with -type f option:

find dir -type f -exec cp {} dest \;