# while loop

**URL:** <https://community.unix.com/t/while-loop/276753>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [November 4, 2010, 7:30pm UTC](https://community.unix.com/t/while-loop/276753 "2010-11-04T19:30:35Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tjmannonline](https://community.unix.com/letter_avatar/tjmannonline/32/5_5575768a8748004e209b776fc1b2916d.png) [@tjmannonline](https://community.unix.com/u/tjmannonline)\
**Post date:** [November 4, 2010, 7:30pm UTC](https://community.unix.com/t/while-loop/276753/1 "2010-11-04T19:30:35Z")

</div>

file1.txt contains:

```nohighlight
mann
mcdevitt
kirkland

```

file2.txt contains:

```nohighlight
mannt
mcdevittd
kirklandv

```

i want to copy /mann/\* /mannt

```nohighlight
cat /tmp/file1.txt | while read file1
do
 
cat /tmp/file2.txt | while read file2
do
cp -rp /tmp/$file1/* /tmp/$file2
 
done
done

```

when i `du -sm /tmp/mannt` , it's bigger than /tmp/mann

it should be identical.

---

<div class="post-metadata">

**Author:** ![phoenix\_nebula](https://community.unix.com/user_avatar/community.unix.com/phoenix_nebula/32/1819_2.png) [@phoenix\_nebula](https://community.unix.com/u/phoenix_nebula)\
**Post date:** [November 4, 2010, 8:08pm UTC](https://community.unix.com/t/while-loop/276753/2 "2010-11-04T20:08:07Z")

</div>

```nohighlight
bash-3.2$ cat 1.txt
bc
bn
nv
bash-3.2$ cat 2.txt
dbc
gnv
kjds
bash-3.2$ paste 1.txt 2.txt |xargs -n2 echo cp
cp bc dbc
cp bn gnv
cp nv kjds
bash-3.2$ paste 1.txt 2.txt |xargs -n2 echo cp |sh

```

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [November 5, 2010, 4:13am UTC](https://community.unix.com/t/while-loop/276753/3 "2010-11-05T04:13:39Z")

</div>

@OP: Your script is copying all files in all directories in file1 to every directory in file2

To read two file in the same loop, it get slightly more complicated:

```nohighlight
while read file1 && read file2 <&3
do
  cp -rp /tmp/"$file1"/* /tmp/"$file2"
done <file1.txt 3<file2.txt

```

---

<div class="post-metadata">

**Author:** ![tjmannonline](https://community.unix.com/letter_avatar/tjmannonline/32/5_5575768a8748004e209b776fc1b2916d.png) [@tjmannonline](https://community.unix.com/u/tjmannonline)\
**Post date:** [November 5, 2010, 8:36am UTC](https://community.unix.com/t/while-loop/276753/4 "2010-11-05T08:36:33Z")

</div>

Scrutinizer,

thank you much. worked perfectly.

what does \<$3 mean ?

\<file1.txt 3\<file2.txt - read from file1 and file2

3\<file2.txt ties up to \<$3 ?

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [November 5, 2010, 8:46am UTC](https://community.unix.com/t/while-loop/276753/5 "2010-11-05T08:46:04Z")

</div>

\<&3 means read from what ever file descriptor 3 is directed to. Indeed, at the bottom of the loop it is defined to read from file2.txt

---

<div class="post-metadata">

**Author:** ![tjmannonline](https://community.unix.com/letter_avatar/tjmannonline/32/5_5575768a8748004e209b776fc1b2916d.png) [@tjmannonline](https://community.unix.com/u/tjmannonline)\
**Post date:** [November 5, 2010, 8:50am UTC](https://community.unix.com/t/while-loop/276753/6 "2010-11-05T08:50:03Z")

</div>

thank you much, that's what i thought. again, thanks.

---

<div class="post-metadata">

**Author:** ![ctsgnb](https://community.unix.com/letter_avatar/ctsgnb/32/5_5575768a8748004e209b776fc1b2916d.png) [@ctsgnb](https://community.unix.com/u/ctsgnb)\
**Post date:** [November 5, 2010, 9:47am UTC](https://community.unix.com/t/while-loop/276753/7 "2010-11-05T09:47:47Z")

</div>

> [@tjmannonline](#):
>
> thank you much, that's what i thought. again, thanks.

use the thanks button then :rolleyes:

---

<div class="post-metadata">

**Author:** ![DGPickett](https://community.unix.com/letter_avatar/dgpickett/32/5_5575768a8748004e209b776fc1b2916d.png) [@DGPickett](https://community.unix.com/u/DGPickett)\
**Post date:** [November 5, 2010, 11:26am UTC](https://community.unix.com/t/while-loop/276753/8 "2010-11-05T11:26:42Z")

</div>

It seems like a shell way to get 'paste'.
