Meaningless Drivel is fun!
The moose likes Linux / UNIX and the fly likes copy recursivley specific file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "copy recursivley specific file" Watch "copy recursivley specific file" New topic

copy recursivley specific file

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
Hi Everybody,

I want to copy only txt files from a directory and its subdirectories using "cp" command.

I reffered to the MAN pages but I didn't get what i needed.

can any one pelase help me out how to copy only text files out of all the files from the sub directories.

cp -r $xxx/yyy/* $aaa/bbb/

this copies all the files including sub directories
i want to copy only txt files

Thanks for helping me


SCJP 1.4
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

Do you want all the files to end up in the same output directory or do you want to copy the original directory structure as well?

An IDE is no substitute for an Intelligent Developer.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11776

Originally posted by saikrishna cinux:
I reffered to the MAN pages but I didn't get what i needed.
All of them? On my computer there are 11,806 man pages. Perhaps if you clarified what you were looking at we could make some suggestions on what might also make sense.

Tim's question is very important.

Since you are stating that you only want text files, you may have to look at some sort of script, possibly combining multiple commands (I would look at find and file and grep).

Some examples of the file command in action:
Regards, Andrew

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Anand Hariharan

Joined: Aug 22, 2006
Posts: 272

Like Tim pointed out, this will copy all the text files in different directories to the same destination directory. However, you were particular that you wanted to use the cp command. Also, I assume that your "txt" files are conveniently named with a .txt "extension". And, yes I cheated by using zsh.

Like Andrew suggests, I suppose one can use the file command to distinguish text from non-text files, but relying on magic isn't foolproof.

- Anand

"Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." -- Antoine de Saint-Exupery
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

Now here's a classic reason for not using shortcut English!

I read this as "txt" files, as in "filename.txt". I saw the "txt" and didn't notice the exact form of the wildcard, which is to scan ALL files. The problem, as has been pointed out, is MUCH bigger when you want all the text files!

For one thing, is an XML file a "text" file for the case in question?
I agree. Here's the link:
subject: copy recursivley specific file
It's not a secret anymore!