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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find all text files in folder and then copy to a new folder
# 1  
Old 06-19-2009
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

Code:
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

Code:
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.
# 2  
Old 06-19-2009
type should be -f...
and in my system, i need to write like find . -depth....
# 3  
Old 06-19-2009
Hey thanks rakeshawasthi.

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

Code:
 
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

Code:
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,
# 4  
Old 06-20-2009
You could try:

Code:
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...
Code:
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...

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


Last edited by Scott; 06-20-2009 at 07:54 AM..
# 5  
Old 06-20-2009
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:

Code:
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

Code:
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

Code:
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


Code:
 
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.

Smilie

Best regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to copy files with the same filenames as those in another folder to that same folder?

Hello All A similar question like this was asked before but I need to change part of the question. I've two folders, Folder A contains some image files in 150 subfolders; Folder B contains text files in 350 subfolders. All image files in Folder A have the same filename as the text... (5 Replies)
Discussion started by: chlade
5 Replies

2. UNIX for Dummies Questions & Answers

Copy files to folder.

Hi, I have a folder which contains some files like this. bin.000001 bin.000002 bin.000003 bin.000004 bin.000005 bin.000129 bin.index I want to copy all these files to a new folder except the last files. Please provide some ideas. Please use next time code tags for your code... (6 Replies)
Discussion started by: arijitsaha
6 Replies

3. Shell Programming and Scripting

copy folder and its contents to another folder

Hi experts, I am coming to you with this basic question on copying a folder and its content from one location to another folder using PERL script. This is my requirement. I have a folder AB under /users/myhome I want to copy AB and its contents to /user/workspace. Finally it should... (1 Reply)
Discussion started by: amvarma77
1 Replies

4. UNIX for Dummies Questions & Answers

How to copy files to one folder?

Hi , I have a file like this, i need to trace its path and copy the files from its path to one folder. I need to replace elib.com,melib.com to F:\.Here i need to copy to a folder called image. Please help http://elib.com/SHC/NLNLHB/020001498.pdf ... (4 Replies)
Discussion started by: umapearl
4 Replies

5. Shell Programming and Scripting

Loop through text file > Copy Folder > Edit XML files in bulk?

I have a text file which contains lines in this format - it contains 105 lines in total, but I'm just putting 4 here to keep it short: 58571,east_ppl_ppla_por 58788,east_pcy_hd_por 58704,east_pcy_ga_por 58697,east_pcy_pcybs_por It's called id_key.txt I have a sample folder called... (9 Replies)
Discussion started by: biscuitcreek
9 Replies

6. Shell Programming and Scripting

Find files of type within folder copy multiple results to different folders

Ok question number two: I'd like to search a directory for multiple file types (rar, txt, deb) and depending on what's found, copy those files to folders named Rar, TextFiles, and Debs. I'm looking for speed here so the faster the script the better. I want it to be a function that I pass 1 argument... (4 Replies)
Discussion started by: DC Slick
4 Replies

7. Shell Programming and Scripting

Copy all zipped files from one folder to another

Hi everyone, when I try to copy *.gz files run cp within the correct source folder it works as follow: Source folder = C:/Documents and Settings/user/Recent papers/2771/ Destination folder = C:/Documents and Settings/user/My documents/1532/temp cp *.gz "C:/Documents and Settings/user/My... (2 Replies)
Discussion started by: cgkmal
2 Replies

8. UNIX for Advanced & Expert Users

Auto copy for files from folder to folder upon instant writing

Hello all, I'm trying to accomplish that if a file gets written to folder /path/to/a/ it gets automatically copied into /path/to/b/ the moment its get written. I thought of writing a shell script and cron it that every X amount of minutes it copies these files over but this will not help me... (2 Replies)
Discussion started by: Bashar
2 Replies

9. Shell Programming and Scripting

How to copy one folder to another with existing files

For example, /tmp/folder1 includes /tmp/folder1/a /tmp/folder1/b /tmp/folder2 includes /tmp/c Is there a command without removing files in /tmp/folder2 first to copy the /tmp/folder1 to /tmp/folder2? and the result should be /tmp/folder2 will include only /tmp/folder2/a... (2 Replies)
Discussion started by: lalelle
2 Replies

10. UNIX for Dummies Questions & Answers

how to copy hidden files from one folder to another

dear all, i want to copy all files in my home dir to another. from my home dir i have given ls -la then some hidden files are there with dot . .. and i also want to copy all dirs in my home as it is . because iam upgrading the system how to copy all files and dirs in my home dir... (1 Reply)
Discussion started by: rajan_ka1
1 Replies
Login or Register to Ask a Question