LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (http://www.linuxquestions.org/questions/linux-software-2/)
-   -   bash command - find |cp (http://www.linuxquestions.org/questions/linux-software-2/bash-command-find-%7Ccp-723749/)

crackpipe 05-04-2009 11:33 PM

bash command - find |cp
 
I wanted to find all the python scripts on my local machine and put them in a directory eg. (/home/crackpipe/pythons), so I can look through them and learn. I'm attempting to learn Python. I tried this command, but it doesn't work, probably for obvious reasons to someone here:
Code:

$ find . -name *.py |cp $1 /home/click/pythons/
cp: missing destination file operand after `/home/crackpipe/pythons'

So it appears "$1" cannot be passed. Not sure what gets passed from "find", if anything. What's a good format to run this? I'm afraid to attempt something like
Code:

$ cp -r *.py /home/crackpipe/pythons
in case it attempts to duplicate the entire disk, or most of it, into /home/crackpipe/pythons. I just want the ".py"'s there. Thanks.

billymayday 05-04-2009 11:39 PM

How about

find . -name *.py -exec cp {} /home/click/pythons/ \;

crackpipe 05-05-2009 12:03 AM

Quote:

Originally Posted by billymayday (Post 3530350)
How about

find . -name *.py -exec cp {} /home/crackpipe/pythons/ \;

Well this was quite right. Many thanks. Now to understand Bash further, something I obviously need work on as well...

ghostdog74 05-05-2009 01:33 AM

since you want to learn Python, here's a Python script that does what the find command does
Code:

import os,shutil
destination="/home/destination"
source="/src"
for r,d,f in os.walk(source):
    for files in f:
        if files.endswith(".py"):
            '''assumes all python files ends with .py'''
            '''if not, extra code to be written to further check'''
            try:
                '''move files to desintation'''
                shutil.move(os.path.join(r,files),os.path.join(destination,files))
            except Exception,e:
                #print e
                pass


chrism01 05-05-2009 02:36 AM

For bash try this doc: http://www.tldp.org/LDP/abs/html/


All times are GMT -5. The time now is 12:30 PM.