This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Journey To Enterprise Agility and have Daryl Kulak & Hong Li on-line!
See this thread for details.
Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

copy recursivley specific file  RSS feed

 
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
 
Bartender
Posts: 19658
92
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?
 
author and jackaroo
Marshal Commander
Posts: 12218
278
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
 
Rancher
Posts: 274
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
Bartender
Posts: 19658
92
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
Boost this thread!