×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Serach for txt files in a direcory and all subdirectories

Serach for txt files in a direcory and all subdirectories

Serach for txt files in a direcory and all subdirectories

(OP)
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!

RE: Serach for txt files in a direcory and all subdirectories

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;

RE: Serach for txt files in a direcory and all subdirectories


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 

RE: Serach for txt files in a direcory and all subdirectories

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;

RE: Serach for txt files in a direcory and all subdirectories

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



Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close