How To Use Find File to Copy

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
SyntheticShield

How To Use Find File to Copy

Post by SyntheticShield »

I know that I can use

Code: Select all

cd `find . -name "filename*" -print`
To change to a directory where a file is. However, how can I use the find file in a script to locate the file and then copy it to another directory?
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
Habitual

Re: How To Use Find File to Copy

Post by Habitual »

cp $(find . -name "filename*" -print) /dir/to/copy/to

There is no need to cd to a directory to find a file however. :wink:

I use find `pwd` myself but here's how it worked on my system:

Code: Select all

find . -name ff.notes
./ff.notes

find `pwd` -name ff.notes
/home/jj/ff.notes

cp $(find `pwd` -name ff.notes) Documents/

find `pwd` -name ff.notes
/home/jj/Documents/ff.notes
/home/jj/ff.notes

Code: Select all

cp $(find . -name ff.notes) Documents/
works too. :)
SyntheticShield

Re: How To Use Find File to Copy

Post by SyntheticShield »

I should have made that a bit clearer. I just cut and pasted from what I was using.

I was using that particular line to move to the directory of an unzipped file after downloading it so that it could be compiled. Ive got a lot to learn and sometimes googling doesnt necessarily provide the clearest of answers. Thanks Habitual for your help.
Habitual

Re: How To Use Find File to Copy

Post by Habitual »

You're Welcome.
Locked

Return to “Scripts & Bash”