Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Serach for txt files in a direcory and all subdirectories

Status
Not open for further replies.

Per9922

IS-IT--Management
Oct 1, 2004
74
SE
I would like to search a directory and all subdirectories and copy all txt files to another directory. How do I do that ?

Thanks all people that are helping out!
 
on Linux just use find

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 

It is not a perl solution, but this command copies everything in the /usr/my directory to the /usr/mynew directory:

Code:
cd /usr/my; find . -depth -print|cpio -pd /usr/mynew

What constitutes a text file - a file with a text extension? You can search just for files with a text extension something like this:

Code:
# UNTESTED
cd /usr/my; find . -type f -name "*.txt" -depth -print|cpio -pd /usr/mynew
 
would it be easier to just use -exec and cp? But I guess I'm assuming he doesn't want to keep the sub directory structure and just dumps them all into new dir?

Just a thought :) I have never used cpio and had to man it to see what -pd did so I could understand your solution.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Travis:

I assumed he meant to copy the directory structure, but you might be right; maybe he wanted all files copied to a seperate directory.

Anyway, back in the day before Linux I used cpio a lot because of portability issues with tar. You can copy a directory structure using tar:
Code:
cd /usr/my; tar cf - .|(cd /usr/mynew; tar xf - )

I chose the the cpio method because I could readily manipulate the find command to pass only txt files.

Ed



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top