LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   extract files from subdirectories to another dir (https://www.linuxquestions.org/questions/linux-newbie-8/extract-files-from-subdirectories-to-another-dir-483730/)

hamboy 09-15-2006 08:01 AM

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!

MensaWater 09-15-2006 09:00 AM

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.


All times are GMT -5. The time now is 06:49 PM.