HelloSilence
HelloSilence

Reputation: 935

linux recursive copy specified files doesn't work

I want to recursive copy all the files which start with letters in directory data to directory test. So I wrote this:

find data -type f -exec grep '^[a-z]' {} \; -exec cp -f {} ./test \;

However, it also matched other files.

What's wrong with the code?

Upvotes: 0

Views: 76

Answers (1)

radical7
radical7

Reputation: 9124

Your command isn't executing grep on filenames, but rather on the contents of those files.

You say:

copy all the files which start with letters in directory

which would use a find command that's matching filenames which requires the -name option. For example,

find data -type f -name '[a-z]*'

By using the -exec option to find, instead you're executing the provided command (grep '^[a-z]' {}) on every file that find finds in the data directory since there is no filename matching clause (-name).

The command you likely want is:

find data -type f -name '[a-z]*' -exec cp -f {} ./test \;

Upvotes: 1

Related Questions