Copy file to two folders with condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy file to two folders with condition
# 1  
Old 03-28-2011
Copy file to two folders with condition

Hello!

Please, help me write this simple script using bash scripting.

Task:

In Folder1 I have files:
name1.txt, name2.txt, name3.txt .. etc
In Folder2 may located such files:
name1.txt, name2.txt .. etc
In Folder3 may located files like:
name1.txt_abc{some_symbols}_vxz, name2.txt_abc{some_symbols}_vxz ... etc - it's the same files as in Folder1 and Folder2, only the names are different.

So, I must to copy files from Folder1 To Folder2, if the file doesn't exist in Folder2 OR Folder3.

Example:
I must to copy file name4.txt to Folder2 if Folder2 doesn't contain name4.txt OR Folder3 doesn't contain file with name like name4.txt_{something}

Thanks!
# 2  
Old 03-28-2011
Here is an untested solution:
Code:
for file $(ls Folder1)
do
  ls Folder2/$file Folder3/$file* 1>/dev/null 2>&1
  [ $? -eq 0 ] || cp Folder1/$file Folder2/$file
done


Last edited by Dahu; 03-28-2011 at 07:27 AM.. Reason: fixed typo
# 3  
Old 03-28-2011
this will help you.........
Code:
cd to $path/folder1
ls -l name*.txt > files_list
for i in `cat files_list`
do
  file_name=$i
  new_file_name='$file_name'_abc{symbol}_vxz
  cp $file_name $path/Folder2/$new_file_name
done


Last edited by Scott; 03-28-2011 at 08:25 AM.. Reason: Code tags
# 4  
Old 03-28-2011
@ Dahu: your solution is incorrect:
Code:
ls Folder2/$file Folder3/$file*

will return non-zero even if $file exists in one of the dirs (but doesn't in the other). You are also missing keyword 'in' in the for loop.

@ rajesh_pola: The quotes around '$file_name' will prevent this variable from expanding. Besides that, you never look into Folder3, instead you hardcoded _abc{symbol}_vxz ...

How about this?
Code:
cd Folder1
for f in * ; do
   [[ -f /path/to/Folder2/$f ]] && echo "$f found in Folder2" && continue
   ls /path/to/Folder3/${f}* &> /dev/null && echo "$f found in Folder3" && continue
   cp $f /path/to/Folder2
done


Last edited by mirni; 03-28-2011 at 08:47 AM..
This User Gave Thanks to mirni For This Post:
# 5  
Old 03-28-2011
Quote:
Originally Posted by mirni
@ Dahu: your solution is incorrect:
Code:
ls Folder2/$file Folder3/$file*

will return non-zero even if $file exists in one of the dirs (but doesn't in the other). You are also missing keyword 'in' in the for loop.
You're right, that why I said untested Smilie, I usually test my scripts before posting but this time I was in an hurry
# 6  
Old 03-28-2011
try this

Code:
 
for file in Folder1/*;do 
f=$(basename $file); 
test -f Folder2/$f || cp $file Folder2/; 
test -f Folder3/${f}_SOMETHING || cp $file Folder3/${f}_SOMETHING ; 
done

# 7  
Old 03-28-2011
thank you, friends Smilie
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/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

2. UNIX for Beginners Questions & Answers

Rsync added new folders after copy?

Hi guys, Don't really know much about unix or anything, just starting to mess around a little bit to have more understanding in general. So, I tried using rsync to copy my macbook pro backup/clone from an external drive I have to another external drive. I ended up using... "sudo rsync -a... (1 Reply)
Discussion started by: cbjeebs
1 Replies

3. HP-UX

Recursive copy of Folders with files

Dear All, I will appreciate any help received. Our system is running on hpux v1 My problem is as follows: We have many customer folders with name fd000100, fd000101 and so on e.g. (Testrun)(testsqa):/>ll /TESTrun/fd000100 total 48 drwxrwx--- 2 fq000100 test 96 Jun 27 2004... (17 Replies)
Discussion started by: mhbd
17 Replies

4. Shell Programming and Scripting

Need to copy all but three folders

Hi, Below is the listing of folder "greece" $ cd greece $ ls tmp server logs properties I wish to copy all the contents of the dir "greece" to /tmp except the below three folders and its contents under "greece" 1.logs 2.server/bin/logs 3. server/cacheI am using sh (normal C... (4 Replies)
Discussion started by: mohtashims
4 Replies

5. Shell Programming and Scripting

Copy between two different folders containing same sub-folders

I have a folder like this ls input1 dir1 dir2 dir3 file1 file2 file3 dir1, dir2 and dir3 are sub-folders inside the folder input1 ls input2 dir1 dir2 dir3 file1 file2 file3 My dir1 in input1 folder has files f1, f2, f3 and f4. My dir1 in input2 folder has file f4 and f5. ... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

6. Shell Programming and Scripting

Copy Folders and ZIP IT

HI All I have one master folder : ABCXYZ I have sub folder in there : AB XY AZ AC PR AL Now i want to copy AB , PR ,AL in to one new folder and zip it with time stamp. like Pre_02192013_12_32.zip Zip folder should be in master folder. (2 Replies)
Discussion started by: pareshkp
2 Replies

7. Shell Programming and Scripting

extracting lines based on condition and copy to another file

hi i have an input file that contains some thing like this aaa acc aa abc1 1232 aaa abc2.... poo awq aa abc1 aaa aaa abc2 bbb bcc bb abc1 3214 bbb abc3.... bab bbc bz abc1 3214 bbb abc3.... vvv ssa as abc1 o09 aaa abc4.... azx aaq aa abc1 900 aqq abc19.... aaa aa aaaa abc1 899 aa... (8 Replies)
Discussion started by: anurupa777
8 Replies

8. UNIX for Advanced & Expert Users

Copy Structure excluding some folders

Hi I want to copy the structure from one place to another. -> cd /hol/; -> find . -type d | cpio -pvdm /abc/cat; while copying the structure I want to exclude some directories like test1 and Test. I have read somewhere that this can be done with -prune option. Could anyone... (2 Replies)
Discussion started by: soumodeep123
2 Replies

9. Shell Programming and Scripting

Copy input file based on condition

Hi, I am new to unix shell programming. I want to write a shell script for a functionality existing in mainframe system. I have one file as below as input 123456 &__987 &12yuq abcdef _ referes to blank condition:whenever the input file is having &__ ,it should be replaced... (4 Replies)
Discussion started by: charan0703
4 Replies

10. Windows & DOS: Issues & Discussions

Copy folders and subfolders from unix to windows

Sir From a unix machine some folders and their folders have to be copied to windows XP PC. Please help me with a batch file or a shell script. I am new to the the shell and batch files. Thanks in anticipation. sastry (3 Replies)
Discussion started by: chssastry
3 Replies
Login or Register to Ask a Question