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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting copy all files with the same filenames as those in another folder
# 1  
Old 06-20-2010
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, regardless of file extension, 100 .txt files in folder B share the same name as those 100 .jpg files, for example 00011020.jpg in folder A is able to find its corresponding 00011020.txt in folder B.

Now, my question is:
How to just copy out the 100 description .txt files that share the same name as those 100 image files, out from folder B into folder C( or create folder C first, and then carry out the copy) ?

Looking forward to your help.

Best Regards
JIA Pei
# 2  
Old 06-20-2010
Code:
for file in ./A
do
temp=`echo $file|cut -d"." -f1`
if [ -r ./B/$temp.txt ]
 then
 cp ./A/$file ./C
 cp ./B/$temp.txt ./C
fi
done

Untested.
# 3  
Old 06-20-2010
Try:
Code:
mkdir -p folderc 2>/dev/null
for i in foldera/*.jpg; do
  f=${i%.*}
  cp -p folderb/"${f##*/}".txt folderc
done

# 4  
Old 06-22-2010
MySQL

Thank you. This one works perfectly.

It's strange I don't have the privilege to tick that "Thanks" button in this board.

Quote:
Originally Posted by Scrutinizer
Try:
Code:
mkdir -p folderc 2>/dev/null
for i in foldera/*.jpg; do
  f=${i%.*}
  cp -p folderb/"${f##*/}".txt folderc
done

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

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

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 files from folder and rename them

hello, I need to build a shell script that receives the folder to copy by parameter and copy all files except thumb.db to another folder and rename them like, file.jpg renamed to file_bb1.jpg. can someone help me Thanks (4 Replies)
Discussion started by: zeker
4 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. 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