LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-10-2003, 11:53 PM   #1
meeshka
Member
 
Registered: Jun 2003
Distribution: Redhat8
Posts: 50

Rep: Reputation: 15
Question Recursive wildcard copying?


This may seem like a stupid question... but I can't figure it out! In a shell, is there a way to recursively copy files of a certain type?

As an example, say you had multiple mp3's in different directories, mixed with other files and you wanted to copy only the mp3's to a single directory.

I thought a command like: 'cp -R *.mp3 newdirectory' would work, but it doesn't. Is there a way to do this? The DOS equivalent would be 'xcopy *.mp3 c:\newdirectoy /S'

Am I missing something or can this not be done?
 
Old 07-11-2003, 01:29 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Read up on the find command: man find

This might be good enough, but you should really check the options available to make sure there isn't another combination thart would fit your needs:

find -type f -name "*.mp3" -exec cp {} /destination \;

Obviously, you need to replace /destination with the full path to the directory you want to put the mp3s in.
 
Old 07-11-2003, 01:54 AM   #3
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,185

Rep: Reputation: 52
since i wanted to answer that question so bad, i'll
elaborate a little.

find starts in the current directory here, since it wasn't
given a directory. you could give it one if you wished.
like

find /mp3 -type f -name "*.mp3" -exec cp {} /destination \;

that would start in the directory mp3 if you wanted.

-type f means type "file" (don't copy devices or links or directories. only files"
that's probably not necessary. your choice.
-name "*.mp3" thats kind of obvious, but will miss MP3

-exec executes the following line for each result find returns
{} replaced with find result
\; terminates -exec line

my preference
find . -name "[Mm][Pp]3" -print -exec cp {} /destination \:
 
Old 07-11-2003, 02:02 AM   #4
meeshka
Member
 
Registered: Jun 2003
Distribution: Redhat8
Posts: 50

Original Poster
Rep: Reputation: 15
Thanks for the reply, looks like a long command - creating a script for this would be a good idea (unless this is already done?).

I thought this might be a very commonly asked question, suprised I couldn't find it easily through Google.

Cheers.

Last edited by meeshka; 07-11-2003 at 02:10 AM.
 
Old 07-11-2003, 02:08 AM   #5
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
I thought this would be a nice "redeemer" for me... The last couple of posts on other threads were serious overkill for the task.

I'm more than willing to share the glory for helping on this thread though...

Yeah, whansard is right about the -name portion, but he left out the wildcard and the dot for the extension. So, combining his and mine would give -name "*.[Mm][Pp]3" which is pretty much guaranteed to get all of them.

I'm just a creature of habit. I add the -type f option as a safety net. Chances are it won't hurt anything, but yeah, you could probably get along without it.

I don't think you need a script. It might be a long command, but it's still only one command. It'll be just as long in a script as it would be on the command line.

---
Edit: You'll have to be careful with the script you've got. You can't use wildcards as your first argument without surrounding it in single quotes. If it's not in single quotes, the shell will expand it before executing your script; that would be a very bad thing for that script

Last edited by Dark_Helmet; 07-11-2003 at 02:13 AM.
 
Old 07-11-2003, 02:12 AM   #6
meeshka
Member
 
Registered: Jun 2003
Distribution: Redhat8
Posts: 50

Original Poster
Rep: Reputation: 15
Yeah, but if its saved as a script it can be used again much quicker, with just a couple of arguments:

/bin/xcp--------------------------------------------

#!/bin/sh
find -type f -name "$1" -exec cp -v {} /$2 \;

-------------------------------------------------------

xcp *.mp3 newdirectory

Last edited by meeshka; 07-11-2003 at 02:14 AM.
 
Old 07-11-2003, 02:18 AM   #7
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
DOH! I wasn't fast enough with the edit...

See the edit on my previous post. An alias is typically used for single command expansion though, but there's nothing that says you can't use a script..
 
Old 07-11-2003, 02:44 AM   #8
whansard
Senior Member
 
Registered: Dec 2002
Location: Mosquitoville
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,185

Rep: Reputation: 52
dangit! i'm mad i missed the *.
i was looking at the brackets too hard.

you might want to use -ok instead of -exec
if you're going to put it in a script
or add a -i to the cp.

i just love the find with the -exec switch and i wanted a
piece of the action. i was testing the stuff i typed in
first to make sure, but i didn't get it typed right in this box.

in unix the long looking commands are the ones that
do the cool stuff.

i don't see as much use for the script cause i'm
always having to type it with different commands.
like bzip2 with txt files
with rar for rar files
with mv and cp
find . -name "*.gz" -exec gzip -d {} \;
find . -name "*.doc" -exec bzip2 {} \;
find . -name "*.txt" -exec rm {} \;
find . -name "*.wav" -exec lame . . . . something something
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
How to setup wildcard DNS RAHUKUMAR Linux - Software 3 11-23-2005 02:02 PM
wildcard vs regular expression lido Linux - Newbie 6 09-02-2004 11:35 AM
I choose you wildcard! tearinox Linux - Newbie 2 01-23-2004 09:44 PM
how to find file using wildcard? huangyanfeng Linux - General 3 10-15-2003 10:21 PM
Wildcard Mail Aliases js_530 Linux - Networking 2 09-11-2003 12:15 AM


All times are GMT -5. The time now is 01:35 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration