user3685285
user3685285

Reputation: 6586

Copying the result of a find operation in shell

I want to find a file, and simultaneously copy it to another directory like this:

cp (find . -name myFile | tail -n 1) dir/to/copy/to

But this says unexpected token `find'

Is there a better way to do this?

Upvotes: 0

Views: 40

Answers (3)

Jatish
Jatish

Reputation: 392

Two options are available-

Appended the missing $() - to evaluate command (not sure the purpose of tail command, only required for samefile in multiple directories)

  1. cp $(find . -name myFile | tail -n 1) dir/to/copy/to

  2. find . -name myFile -type f -exec cp {} dir/to/copy/to \;

Upvotes: 0

user3914897
user3914897

Reputation: 115

find . -name 'myFile' -print0 | tail  -n 1 | xargs -0 -I {} cp {} /dir/to/copy/to/

Upvotes: 0

anubhava
anubhava

Reputation: 785276

You may use a pipeline:

find . -name 'myFile' -print0 | tail -z -n 1 | xargs -0 -I {} cp {} /dir/to/copy/to/

Using -print0 option to address filenames with whitespace, glob characters

Upvotes: 1

Related Questions