Copy files from unknown sub-folder


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Copy files from unknown sub-folder
# 1  
Old 01-20-2011
Copy files from unknown sub-folder

Hi,

I want to copy files from a unknown sub-folder to other sub-folder without copying the sub-folder. I tried the command:

find . -name Results*.pdf | cp *.* /home/dario/Desktop/

but i always copy the subfolder.

Can anybody help?
# 2  
Old 01-20-2011
"cp" doesn't read from pipe, so the find is useless. What you're doing just amounts to cp *.* /home/dario/Desktop/

Try cp Results*.pdf /home/dario/Desktop/

Or if you're trying to find Results*.pdf in many subdirectories:
find ./ -print0 -name 'Results*.pdf' | xargs --null tar -cf - | tar -C /home/dario/Desktop -xf -

What this does is stuff every file into a TAR archive, and simultaneously extract the tar onto your desktop. This preserves the directories they're found in.

xargs converts pipes into arguments. "echo a b c d | xargs cat y" amounts to "cat y a b c d".

The -print0 and --null are necessary to prevent xargs from choking on filenames and paths with spaces.

By the way: *.* is a DOS-ism, UNIX doesn't have fixed file extensions like that. *.* would match a file named "file.name" but would not match a file named "filename", since it wouldn't find the dot. If you want to really match everything, just * by itself will do.

Last edited by Corona688; 01-20-2011 at 12:06 PM..
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 01-20-2011
The command copies all the sub-folders. I just want to copy pdf files inside the sub-folders and not the sub-folders
# 4  
Old 01-20-2011
Are you sure? What if some of them have the same name? They couldn't all exist in the same directory.

cp isn't capable of creating directories unless you gave it the -R parameter, so that can't be all of what you actually typed.

If you're absolutely positive there's no duplicate names:
Code:
find ./ -name 'Result*.pdf' | while read FILE ; do cp "$FILE" /home/dario/Desktop/ ; done

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 01-20-2011
I'm sure. And even if there are any files with the same name, I'm absolutely sure that they are the copy of the same file. That command worked perfectly. Thanks
This User Gave Thanks to limadario For This Post:
# 6  
Old 01-21-2011
Code:
find . -name 'Result*.pdf' -type f -exec cp {} /home/dario/Desktop/ \;

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

4. Shell Programming and Scripting

Copy files from one folder to another with rule

Hello! Please, help me to find or write this simple bash-script. I have first folder /tmp/work/folder1 with such files: name1.txt name2.txt.1 name2.txt.2 name3.txt name4.txt name4.txt.1 name4.txt.2 name4.txt.3 etc.. I need to copy all files from folder1 to folder... (1 Reply)
Discussion started by: optik77
1 Replies

5. 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

6. Shell Programming and Scripting

copy all files with the same filenames as those in another folder

Hi, all: I've got two folders, folder A contains some image files (say, 100 files) in .jpg format; folder B contains all description files (say, 500 files) in .txt format. All image files in folder A are able to find their corresponding description files in folder B. That is to say,... (3 Replies)
Discussion started by: jiapei100
3 Replies

7. Shell Programming and Scripting

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 ... (4 Replies)
Discussion started by: cgkmal
4 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