Find all text files in folder and then copy to a new folder

Hi all,

*I use Uwin and Cygwin emulator.

I�m trying to search for all text files in the current folder (C/Files) and its sub folders using

find  -depth -name "*.txt" 

The above command worked for me, but now I would like to copy all found text files to a new folder (C/Files/Text) with

find  -depth -name "*.txt" -type d -exec cp {} Text \;

But this command it�s not working even when replace Text with C/Files/Text.

Please somebody could give an advice about this.

Thanks in advance.

type should be -f...
and in my system, i need to write like find . -depth....

Hey thanks rakeshawasthi.

I changed to option -f and didn�t work for me.

 
find  -depth -name "*.txt" -type -f -exec cp {} Text \; #doesn�t show any pritn out.
("Text" is the folder where I want to send the found files and I�m not sure if it is the correct syntax).

the first part works percfectly

find  -depth -name "*.txt" # Works nice showing found files

But my attempt to copy the txt files found to a new folder is failing
I don�t know why.

Maybe you experts could solve this pending issue to my problem.

Thanks in advance,

You could try:

tar cf - $(find . -name *.txt) | (cd /new_dir; tar xf -)
  1. I don't know how it might work with Uwin / Cygwin as I never use them
  2. It might break if the arg list is too big (i.e. when you have too many txt files)
  3. It keeps the same heirarchy (which you may not want)

So...

find . -name *.txt -exec cp {} Test \;

as you originally had, should work fine!

It doesn't print any output because you didn't ask it to...

find . -name *.txt -print -exec cp {} Test \;

Hi scottn,

Thanks for your very good answer. It help me a lot to follow what I wanted. It looks the option "-type" was not correct in this search in my original script.

Obtained results:

find . -name "*.txt" -print -exec cp {} "C:\My Dir\New Dir" \;# It works
#In Uwin -name is insentive, takes *txt and *TXT, but Cygwin only takes *.txt
find . -name "*.txt" -exec cp {} "C:\My Dir\New Dir" \; # It works
#In Uwin -name is insentive, takes *txt and *TXT, but Cygwin only takes *.txt
find -depth -name "*.txt" -exec cp {} "C:\My Dir\New Dir" \; # It works
#In Uwin -name is insentive, takes *txt and *TXT, but Cygwin only takes *.txt
 
tar cf - $(find . -name *.txt) | (cd "C:\My Dir\New Dir"; tar xf -)# Doesn�t work for me for reasons below
#find: paths must precede expression: input1.txt
#Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|e
#path...] [expression]
#tar: Cowardly refusing to create an empty archive
#Try `tar --help' or `tar --usage' for more information.
#tar: This does not look like a tar archive
#tar: Exiting with failure status due to previous errors

rakeshawasthi and scottn, thanks both for help to get my desired result.

:b:

Best regards