Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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'
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
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 01:13 AM.
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..
Distribution: RH 6.2, Gen2, Knoppix,arch, bodhi, studio, suse, mint
Posts: 3,304
Rep:
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.