Did you know LQ has a Linux Hardware Compatibility List?
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 09-15-2006, 08:01 AM   #1
LQ Newbie
Registered: Sep 2006
Posts: 1

Rep: Reputation: 0
extract files from subdirectories to another dir


i'm wondering if cp has this feature :
i want to extract *.txt files in a directory with several subdirectories containing *.txt files


i need to extract and copy all *.txt files in /usr/options/textfiles/ into one directory, ie hav all the *.txt files extracted from their folders

the cp -r copies everthing (subfolders and files).. is there a way just to extract the files?

i'm sorta want to do this..
find /usr/options/textfiles | grep .txt
copy (the result of find) to /usr/text/

Old 09-15-2006, 09:00 AM   #2
LQ Guru
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,223
Blog Entries: 10

Rep: Reputation: 835Reputation: 835Reputation: 835Reputation: 835Reputation: 835Reputation: 835Reputation: 835
A simple for loop will do it:

for FILE in `find /usr/options/textfiles -type f`
do cp -p $FILE /usr/text
The "type -f" says to find only regular files so would omit directory names (as well as block and character devices and symbolic links).

The "cp -p" says to copy it with the same modification date, permissions, ownership and group as the original.

FILE is an abitrary variable name which is later referenced as $FILE. You could use FRED and $FRED instead if you preferred - the name doesn't matter so long as it isn't a reserverd variable like PATH.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Q. moving /bin dir files to /dev dir?? Texas_student Linux - Software 2 03-26-2006 11:42 PM
copy 3 files to a directory and all subdirectories? snip128 Linux - Newbie 9 08-12-2005 09:18 PM
Unzipping all .gz files in all subdirectories darin3200 Linux - General 10 04-24-2004 09:58 PM
move files to a series of subdirectories acid2000 Linux - General 1 09-01-2003 05:44 PM
delete files / keep subdirectories? lhorstman Linux - Newbie 2 01-10-2003 03:01 PM

All times are GMT -5. The time now is 04:00 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration