How To Use Find File to Copy

Forum rules
Before you post please read how to get help

How To Use Find File to Copy

Postby SyntheticShield on Fri Jul 29, 2011 2:45 pm

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?
SyntheticShield
Level 1
Level 1
 
Posts: 43
Joined: Wed Dec 09, 2009 10:20 am

Linux Mint is funded by ads and donations.
 

Re: How To Use Find File to Copy

Postby Habitual on Fri Jul 29, 2011 2:52 pm

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. :)
What's a landing but a take off in reverse?
User avatar
Habitual
Level 8
Level 8
 
Posts: 2252
Joined: Sun Nov 21, 2010 8:31 pm
Location: LM17Q-Xfce

Re: How To Use Find File to Copy

Postby SyntheticShield on Fri Jul 29, 2011 2:59 pm

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.
SyntheticShield
Level 1
Level 1
 
Posts: 43
Joined: Wed Dec 09, 2009 10:20 am

Re: How To Use Find File to Copy

Postby Habitual on Fri Jul 29, 2011 3:00 pm

You're Welcome.
What's a landing but a take off in reverse?
User avatar
Habitual
Level 8
Level 8
 
Posts: 2252
Joined: Sun Nov 21, 2010 8:31 pm
Location: LM17Q-Xfce


Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 4 guests