LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-15-2004, 06:12 PM   #1
quentusrex
Member
 
Registered: Aug 2004
Location: Seattle, Washington
Posts: 101

Rep: Reputation: 15
Coding question


I'm trying to copy all the files in one partition to a folder in another partition. I want all the files that have a certain phrase in the file name to be copied. for instance: I want all files with 'school' in the file name to be copied to the folder /home/backups/school. what command would work best for this?

I was thinking something like this:

find|grep school| cp (stdout) /home/backups/school

but my problem is that I don't know how to manipulate the stdout to be placed in that position. any ideas?
 
Old 08-15-2004, 06:53 PM   #2
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
you don't need grep, find can do pattern matching with -name or -iname arg. something like: 'find /path -iname '*school*' -exec cp /home/backups/school "{}" ";"'
 
Old 08-15-2004, 07:06 PM   #3
quentusrex
Member
 
Registered: Aug 2004
Location: Seattle, Washington
Posts: 101

Original Poster
Rep: Reputation: 15
I am trying to enter this command into the shell as a bash command. the format you gave doesn't work.

please be specific. i'm relatively new...
 
Old 08-15-2004, 07:13 PM   #4
Charalambos
Member
 
Registered: Aug 2004
Location: Switzerland
Distribution: debian
Posts: 149

Rep: Reputation: 15
I suggest you to use the command xargs.
Then you would have something like this:
--> find | grep school | xargs -n1 -i cp -r {} /home/backups/school

The curly braces mean for every argument you get to the pipe.

BTW infamous41md: -name does not search for a string inside a file, but searches a file that conatins in its name that string (-iname is the same but for case insensitive).

Last edited by Charalambos; 08-15-2004 at 07:14 PM.
 
Old 08-15-2004, 07:17 PM   #5
infamous41md
Member
 
Registered: Mar 2003
Posts: 804

Rep: Reputation: 30
he said "a certain phrase in the filename", and the example i gave did just that. quantus, the command i gave works fine. maybe u copied it wrong. i suggest reading the man page find, type 'man find' [enter].
 
Old 08-15-2004, 07:23 PM   #6
Charalambos
Member
 
Registered: Aug 2004
Location: Switzerland
Distribution: debian
Posts: 149

Rep: Reputation: 15
Oops, sorry, you're right, i thought he was talking about searching inside the file. My fault.
 
Old 08-15-2004, 07:30 PM   #7
quentusrex
Member
 
Registered: Aug 2004
Location: Seattle, Washington
Posts: 101

Original Poster
Rep: Reputation: 15
ok, i copied the text exactly. and entered it into the shell prompt. all it did was bring me to the " > " prompt.


guys, my question was that is there any way to pipe the stdout into a command.

for instance, if {} were the way to pipe all stdout into a certain spot, then this command would retrieve all log files and copy them to a certain directory.

find / | grep .log|cp {} /logbackups/today


see? i'm trying to learn how to master the stdout manipulation. and i haven't found anything related to the above situation in any tutorial. thanks.
 
Old 08-16-2004, 04:19 AM   #8
dannyp
LQ Newbie
 
Registered: Mar 2004
Location: Philadelphia, PA
Posts: 14

Rep: Reputation: 0
Why not just do cp -r /*school* /destination
 
Old 08-16-2004, 04:59 AM   #9
cryptwizard
Member
 
Registered: Aug 2004
Location: Sydney, Australia
Distribution: Debian Squeeze
Posts: 49

Rep: Reputation: 15
yeah that's a great idea.
you might need /*school*.*

and add the -f flag to avoid any mass of annoying questions:

cp -f -r /mnt/(windows)/Documents\ and\ Settings/(user)/My\ Documents/*school*.* /dir/to/copy/to

replace appropiate stuff.
notice i escaped spaces with \
 
Old 08-17-2004, 07:53 AM   #10
rkdugar
LQ Newbie
 
Registered: Jul 2004
Posts: 13

Rep: Reputation: 0
If u r trying to learn how to master the stdout manipulation, then u can use the following command:

$ find /path -iname '*school*.*' -exec cp "{}" /bkup_path ";"

Cheers,

rkdugar
 
Old 08-17-2004, 08:16 AM   #11
quentusrex
Member
 
Registered: Aug 2004
Location: Seattle, Washington
Posts: 101

Original Poster
Rep: Reputation: 15
that command is excellent. it worked wonders. with only one hitch. it doesn't respond well to spaces....

Program Files.....

any ideas how to pipe quotation marks before and after the stdout? would something like this work?


# find /mnt/C -iname '*school*.*' -exec cp /""{}"/" /home


???
because i'm sure it'd work if quotes could somehow be appended and prepended to the sdtout. thank you soo much for that last command.
 
Old 08-17-2004, 09:03 AM   #12
rkdugar
LQ Newbie
 
Registered: Jul 2004
Posts: 13

Rep: Reputation: 0
The appended and prepended quotes to the sdtout will not solve ur purpose. You can try the following command:

# find /mnt/C -iname '*school*' -exec cp "{}" /home ";"


You can use the following comand, if u want to copy some selected files:

# find /mnt/C -iname '*school*' -ok cp "{}" /home ";"

It should work...and don't forget to give ";" at the end of the command.

Cheers,

rkdugar
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Coding a WM? SocialEngineer Programming 1 05-20-2005 08:32 AM
MFC Coding Ameii83 Programming 3 12-22-2004 10:53 PM
Coding question quentusrex Debian 6 08-16-2004 04:42 AM
html coding help? breakerfall Programming 13 07-07-2003 07:22 PM
irc coding nautilus_1987 General 1 12-07-2002 10:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration