LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices

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

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


hi...

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

ie.
/usr/options/textfiles/aug25/aug25.txt
/usr/options/textfiles/aug26/aug26.txt
/usr/options/textfiles/aug27/aug28.txt
/usr/options/textfiles/aug28/aug27.txt
/usr/options/textfiles/dates.txt

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/

thanks!
 
Old 09-15-2006, 09:00 AM   #2
MensaWater
Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 5,950
Blog Entries: 5

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

Code:
for FILE in `find /usr/options/textfiles -type f`
do cp -p $FILE /usr/text
done
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.
 
  


Reply


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 05:59 AM.

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