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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to copy files with the same filenames as those in another folder to that same folder?
# 1  
Old 09-19-2013
Question 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 files in Folder B.


That is to say, regardless of file extension, 10000.txt files in subfolders of Folder B share the same filename as 10000.tif files in subfolders of Folder A. For example 00011020.tif in subfolder 0120 of Folder A is able to find its corresponding 00011020.txt file in subfolder 0300 of Folder B. So same filename are not in the corresponding subfolders.

My question is:


How to copy the text files that share the same filename as those image files to the corresponding subfolders in Folder A. I want to combine all files in the subfolders of Folder A.


Thanks in advance for your help.

Chlade
# 2  
Old 09-19-2013
Using shell script
Code:
#!/usr/bin/ksh
for TXTFILE in $(find FolderB -type f -name "*.txt")
do
 BASE=$(echo $(basename $TXTFILE) | cut -d. -f 1)
 TIFFILE=$(find FolderA -type f -name "${BASE}.tif")
 echo cp $TXTFILE $(dirname $TIFFILE)
done

Replace the red foldernames with actual directory names.

After reviewing the output you may remove the blue echo to actually copy the text files to corresponding image directories.
# 3  
Old 09-19-2013
Code:
find . -name *.tif | xargs -I % bash -c 'val=%; find . -name $(basename ${val/.tif/.txt}) | xargs -I % cp % $(dirname $val) '

Run from root directory common to both folders or replace the . in find command with required folder names.

--ahamed

Last edited by ahamed101; 09-19-2013 at 04:02 AM..
# 4  
Old 09-19-2013
Quote:
Originally Posted by krishmaths
Using shell script
Code:
#!/usr/bin/ksh
for TXTFILE in $(find FolderB -type f -name "*.txt")
do
 BASE=$(echo $(basename $TXTFILE) | cut -d. -f 1)
 TIFFILE=$(find FolderA -type f -name "${BASE}.tif")
 echo cp $TXTFILE $(dirname $TIFFILE)
done

Replace the red foldernames with actual directory names.

After reviewing the output you may remove the blue echo to actually copy the text files to corresponding image directories.
The first echo is not needed.
Code:
BASE=$(basename $TXTFILE | cut -d. -f 1)

# 5  
Old 09-19-2013
Quote:
Originally Posted by MadeInGermany
The first echo is not needed.
Code:
BASE=$(basename $TXTFILE | cut -d. -f 1)

And, if you have a filename like a.b.txt, this will give you a while I expect that you really want a.b. So, BASE=$(basename $TXTFILE .txt) would be better. But the loop could be made more efficient using:
Code:
#!/usr/bin/ksh
for TXTFILE in $(find FolderB -type f -name "*.txt")
do
 BASE=${TXTFILE%.txt}
 TIFFILE=$(find FolderA -type f -name "${BASE}.tif")
 echo cp $TXTFILE ${TIFFILE%/*}
done

But, even if there is only one file in each of the 350 subdirectories in folderB, this will still be running find 351 times (which will make this a very slow script). And, if there is a missing .txt file, you won't know about it; and if there is a missing .tif file you'll have a malformed cp command.

The following script runs find twice, and awk once no matter how many files need to be copied and prints diagnostics for missing .txt and .tif files:
Code:
#!/usr/bin/ksh
(find folderB -name '*.txt';find folderA -name '*.tif') | awk -F/ '
/txt$/ {d[substr($NF, 0, length($NF)-4)] = substr($0, 0, length($0)-length($NF))
        next
}
{       b = substr($NF, 0, length($NF) - 4)
        if(b in d) {
                printf("cp \"%s\" \"%s\"\n", $0, d[b])
                delete d[b]
        } else {
                printf("printf \"No .txt file for %%s\\n\" \"%s\" >&2\n", $0)
        }
}
END {   for(i in d) 
                printf("printf \"No .tif file for %%s\\n\" \"%s\" >&2\n",
                        d[i] i ".txt")
}' | /usr/bin/ksh

I suggest that you remove the | /bin/ksh from the last line of the script until you see that it will do what you want. (Note that the awk script is printing shell commands to be run by the shell; not running the commands themselves.)

You may need to change the pathname to the Korn shell (in both the 1st and last line of the script) if you move to a different system. And, if you're using a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
# 6  
Old 09-25-2013
Everybody, thanks for the responses. I'm an idiot. I was so happy to find someone with my issue that I didn't bother looking at the OS. I needed this for Windows. I've found a solution for Windows.

Thanks for your help and sorry for wasting your time.

Chlade
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Renaming files & folder according to the similarities in filenames

hello does someone want to help me for this one ? i want to rename files & a folder according to the similarities in filenames for example : the file with the good name cglogo tougl1953 dgmel bogd 01 -- ttgductoog ggdté gollogtd.ext1the others files needed to be renamed cglogo... (5 Replies)
Discussion started by: mc2z674gj
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. Shell Programming and Scripting

HELP! Need to compare 2 folders on 2 different systems, and copy unmatched filenames to other folder

This has been tearing my hair out. I need to: 1: compare server1:/data/archive/ to server2:/data/archive/ (through rsync, ssh, etc) 2: filenames that don't match, get copied (scp) to server2:/data/ server1 and server2 have ssh, scp, rsync access between eachother. Is there any option in... (3 Replies)
Discussion started by: damang111
3 Replies

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

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

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

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

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

10. 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
Login or Register to Ask a Question