Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

copy recursivley specific file

 
saikrishna cinux
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18152
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11878
195
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Anand Hariharan
Rancher
Posts: 272
C++ Debian VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18152
52
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic