• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

copy recursivley specific file

 
Ranch Hand
Posts: 689
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 280
VI Editor C++ Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 27763
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
It would give a normal human mental abilities to rival mine. To think it is just a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic