Copying folders with only certain files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Copying folders with only certain files
# 1  
Old 02-06-2015
Copying folders with only certain files

Hello gurus, quick question

I have a bunch of folders each having subfolders that have 3 files with the same name but different in content. So I want to copy the subfolders with only those 3 files to another location. Just making up some names here,

Code:
Folder1
SubfolderX1
SubfolderY1


Folder2
SubfolderX2
SubfolderY2
SubfolderZ2

Folder3
SubfolderX3
SubfolderY3
SubfolderZ3

I want files file1, file2 and file3 inside each of these subfolders.

I want to copy all these subfolders to a different folder say Folder4
Code:
Folder4
SubfolderX1
SubfolderY1
SubfolderX2
SubfolderY2
SubfolderZ2
SubfolderX3
SubfolderY3
SubfolderZ3

with each of these having file1, file2 and file3...and nothing else...


This is what I tried

Code:
for f in Folder1 Folder2 Folder3;  do  
 cp -R $f/Subfolder* Folder4/ ; 
done


Update

Was able to solve this, thanks..

Code:
 find . -type f -not \( -name 'file1' -or -name 'file2'   -or -name 'file3'  \) -delete


Last edited by ritakadm; 02-06-2015 at 01:52 PM..
# 2  
Old 02-21-2015
Do you have to worry about name collisions, where FolderA and FolderB each contain SubfolderNN?

Also, your method copies everything and then deletes what you don't want. How about just copying what you want:
Code:
for dir in Folder1 Folder2 Folder3; do
  cd ${dir}
  find . -type f \( -name File1 -o ... -o -name FileN \) -print0 | cpio -p0dmu ../Folder4
  cd ..
done

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Copying specific file types to specific folders

I am trying to write a script that cycles through a folder containing many folders and when inside each one it's supposed to copy all the .fna.gz files to a folder elsewhere if the file and the respective folder have the same name. for fldr in /home/playground/genomes/* ; do find .... (8 Replies)
Discussion started by: Mr_Keystrokes
8 Replies

2. Shell Programming and Scripting

How to copy files/folders and show the files/folders?

Hi, So i know we use cp -r as a basic to copy folders/files. I would like this BUT i would like to show the output of the files being copied. With the amazing knowledge i have i have gone as far as this: 1) find source/* -exec cp -r {} target/ \; 2) for ObjectToBeCopied in `find... (6 Replies)
Discussion started by: Imre
6 Replies

3. Shell Programming and Scripting

Copying files from various folders to similar folder structure in another location

Hi, I need to write a script the has to copy the files from folders and subfolders to the same folder structure located in another location. Ex: mainfolder1 file1,file2,file3 subfolder1(file1,etc) subfolder2(file1,etc) to another folder location of same folder structure. rsync is not... (7 Replies)
Discussion started by: Raji Perumal
7 Replies

4. UNIX for Advanced & Expert Users

Copying excluding some files and folders

I have a main folder. Inside that i have many(50) subfolders. In each subfolder, there are a no of large files(500 files ) present. I want to do copy operation for some files from some of the subfolders to /usr/tmp. I have the list of the subfolders and list of of files which i dont want to... (4 Replies)
Discussion started by: millan
4 Replies

5. Programming

Copying folders from one location to another..

Hi all, I need a suggestion in the following case, I have folder1, folder2 ,folder3 and file1 inside /home/test/source .. I need to copy all the folders and files to another location /home/test/destination Pls suggest any way to program this in C++.. :confused: (1 Reply)
Discussion started by: selvarajvs
1 Replies

6. UNIX for Dummies Questions & Answers

Searching for folders/parent folders not files.

Hello again, A little while back I got help with creating a command to search all directories and sub directories for files from daystart of day x. I'm wondering if there is a command that I've overlooked that may be able to search for / write folder names to an output file which ideally... (2 Replies)
Discussion started by: Aussiemick
2 Replies

7. OS X (Apple)

Automated command ; extracting files from folders and copying them into a single folder

Hello everyone, I'm running Mac OS X Leopard (10.5.8) and I want to use the Terminal to help automate this tedious and laborious command for me: I need to extract all of the .m4p files in my "iTunes Music" folder which reside in folders of the artist, and then subfolders for the albums and... (2 Replies)
Discussion started by: qcom
2 Replies

8. UNIX for Dummies Questions & Answers

Copying multiple folders to local machine (don't know folder names)

Hi. I'm trying to copy multiple folders from the remote machine to the local machine. I wrote a batch file to run an ftp window. The problem I am having is that the only command to copy files is mget *, and this copies only files, not folders. For example, ftp ts555 cd ts555/test ' test... (5 Replies)
Discussion started by: leenyburger
5 Replies

9. Shell Programming and Scripting

Copying specific files from remote m/c to specific folders

Hi All, I am trying to rsync some of the latest files from remote m/c to my local linux box. Folder structure in my remote m/c looks like this /pub/Nightly/Package/ROLL/WIN /pub/Nightly/Package/SOLL/sol /pub/Nightly/Package/SOLL/linux Each of the folder contains gzip files which on daily... (0 Replies)
Discussion started by: jhoomsharabi
0 Replies

10. UNIX for Dummies Questions & Answers

Copying Folders without some folders... ;-)

I am in a fix....... I have to write a backup script to backup say Folder A. Folder A contains n folders 1,2 ,3 .....n. my script should copy A without folder 2 & 3. Is there anyway I can do it without writing individual copy commands???? Please help.... (5 Replies)
Discussion started by: chimpu
5 Replies
Login or Register to Ask a Question