Score:-4

Copy some files from source directory A to directory B and some files to Directory C with non sequential order

ki flag

I am looking for a script to copy files from a source directory to two destination directories with non sequential order.

For instance I want to copy some files from source directory A/ to destination directory B/ and copy the remaining files to destination directory C/. It would be desirable as well if the files in directory A/ are equally divided between directory B/ and directory C/.

I have something like this:

$ tree A/ B/ C/
A/
├── file0
├── file1
├── file2
├── file3
├── file4
├── file5
├── file6
├── file7
├── file8
└── file9
B/
C/

And I want it to be like this:

$ tree A/ B/ C/
A/
├── file0
├── file1
├── file2
├── file3
├── file4
├── file5
├── file6
├── file7
├── file8
└── file9
B/
├── file1
├── file3
├── file5
├── file7
└── file9
C/
├── file0
├── file2
├── file4
├── file6
└── file8
sunman avatar
ki flag
@Raffa I have one directory where files are coming.I need to copy some of these files to directort B and some files to directory C to avoid duplication.
Raffa avatar
jp flag
Still not clear ... Duplicate filenames can't co-exist in the same directory "A" ... So, this can not be your problem or you did not describe the problem correctly ... [edit] your question to describe the problem and how it might happen and show examples where possible please.
sunman avatar
ki flag
@Raffa actually I have to copy files from source folders to two different folders but not in consecutive order. Some files to folder b and some file to folder c hope I make it clear
Raffa avatar
jp flag
Still not clear :-) ... I have added an answer below ... please look at it and try it ... It will ether solve your problem or it will help you to explain your problem.
Score:2
jp flag

This code will copy one file to B/ and the next file to C/ and repeats until all files in A/ are copied:

for f in A/*; do
i=$((i+1))
o=$((i%2))
[[ "$o" -eq 0 ]] && cp -v -- "$f" B/ || cp -v -- "$f" C/
done

This is a demonstration:

ubuntu@Lenovo:~/test$ mkdir A B C
ubuntu@Lenovo:~/test$ touch A/file{0..9}
ubuntu@Lenovo:~/test$ tree A/ B/ C/
A/
├── file0
├── file1
├── file2
├── file3
├── file4
├── file5
├── file6
├── file7
├── file8
└── file9
B/
C/

0 directories, 0 files
ubuntu@Lenovo:~/test$ for f in A/*; do
i=$((i+1))
o=$((i%2))
[[ "$o" -eq 0 ]] && cp -v -- "$f" B/ || cp -v -- "$f" C/
done
'A/file0' -> 'C/file0'
'A/file1' -> 'B/file1'
'A/file2' -> 'C/file2'
'A/file3' -> 'B/file3'
'A/file4' -> 'C/file4'
'A/file5' -> 'B/file5'
'A/file6' -> 'C/file6'
'A/file7' -> 'B/file7'
'A/file8' -> 'C/file8'
'A/file9' -> 'B/file9'
ubuntu@Lenovo:~/test$ tree A/ B/ C/
A/
├── file0
├── file1
├── file2
├── file3
├── file4
├── file5
├── file6
├── file7
├── file8
└── file9
B/
├── file1
├── file3
├── file5
├── file7
└── file9
C/
├── file0
├── file2
├── file4
├── file6
└── file8

0 directories, 5 files
sunman avatar
ki flag
Thank you so much for your reply Yes thats right this is what i want.. But please tell me what if i copy files from one server directory A to multiple servers with same directory structure the way you mentioned directory structure A B and C directories I have already setup RSA public key for integration between servers for no password require only thanks in advance
Raffa avatar
jp flag
@sunman For copying to directories on other servers, you can use `scp` instead of `cp` … please see an example here: https://unix.stackexchange.com/a/106482
sunman avatar
ki flag
dear like you propose me the answer above with same scanario but instead of folder i need to copy to servers. i have emailed you plz check
Raffa avatar
jp flag
@sunman Please, ask a new question and include the bash code from this answer and state that you want to do the same but, copy from directory `A/` on the local machine to directories `B/` and `C/` that are located on other servers ... Please also include if the servers are on the same LAN with any other helpful information ... Meanwhile, please [accept](https://meta.stackexchange.com/a/5235/556770) the answer above so that I can fix this question and make it clearer and more useful to other users ... Then I will check your new question and try to help :-)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.