wood burning stoves 2.0*
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
Author

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

Sai


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15952
    
  19

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?


Customer surveys are for companies who didn't pay proper attention to begin with.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11404
    
  81

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
Rancher

Joined: Aug 22, 2006
Posts: 257



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: 15952
    
  19

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?
 
Don't get me started about those stupid light bulbs.
 
subject: copy recursivley specific file
 
Similar Threads
How to list all directories in a webapp
classpath
how to search and return the existence of a file of a particular type on a path
Not able to put some files inside Zip file
Zip file creation with directory suppression through an API