# Linux BASH/Shell question

**URL:** <https://boards.straightdope.com/t/linux-bash-shell-question/601740>\
**Category:** Factual Questions\
**Created:** [November 3, 2011, 2:02pm UTC](https://boards.straightdope.com/t/linux-bash-shell-question/601740 "2011-11-03T14:02:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aaelghat](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@aaelghat](https://boards.straightdope.com/u/aaelghat)\
**Post date:** [November 3, 2011, 2:02pm UTC](https://boards.straightdope.com/t/linux-bash-shell-question/601740/1 "2011-11-03T14:02:03Z")

</div>

I have a set of files on a CentOS server that are in various sub-folders, and what I want to do is copy those files to a different server and sort them alphabetically by the folder they were in, and within that alphabetically by the file name.

The folders have names like First, Group01, Group02, Inbox, Last, and so I would want them sorted in that order.

What I need it to do is move and rename the files from:

/cd/cdart/11581/audio/files/Group01/msg0001.wav  
/cd/cdart/11581/audio/files/Group01/msg0002.wav  
/cd/cdart/11581/audio/files/Group02/msg0001.wav

to here  
/cd/cdart/11581/audio/files/Group01\_msg0001.wav  
/cd/cdart/11581/audio/files/Group01\_msg0002.wav  
/cd/cdart/11581/audio/files/Group02\_msg0001.wav etc

by pre-pending the directory name to the file so that the files are moved into a new directory.

I think it needs a nested loop to get the original files move them and rename them.

In BASH I am trying this :

find /home/123456/public\_html/cd/cdart/11581/audio -maxdepth 1 -name “Group\*” -type d | while read -r dir do  
cp “{dir}/\*" "/home/123456/public\_html/cd/cdart/11581/files/{dir##\*/}.wav”  
done

If you try this:

find /home/123456/public\_html/cd/cdart/11581/audio -maxdepth 1 -name “Group\*” -type d | while read -r dir do  
cp “{dir}/msg0001.wav" "/home/123456/public\_html/cd/cdart/11581/files/{dir##\*/}.wav”  
done

It _sort of_ works but it only pulls msg0001.wav from each of the  
Group\* folders… thus producing files  
/cd/cdart/11581/audio/files/Group01.wav  
/cd/cdart/11581/audio/files/Group02.wav  
/cd/cdart/11581/audio/files/Group03.wav  
etc.  
which is not correct.

Any ideas on how to accomplish what I’d like to do? Thanks.

---

<div class="post-metadata">

**Author:** ![Sizzles](https://avatars.discourse-cdn.com/v4/letter/s/e480ec/32.png) [@Sizzles](https://boards.straightdope.com/u/Sizzles)\
**Post date:** [November 3, 2011, 3:57pm UTC](https://boards.straightdope.com/t/linux-bash-shell-question/601740/2 "2011-11-03T15:57:59Z")

</div>

How about;

find . | grep Group | awk ‘BEGIN { FS="/"} { printf("%s %s/%s\_%s  
",$0, $1, $2, $3)}’ | xargs -n 2 mv

---

<div class="post-metadata">

**Author:** ![aaelghat](https://avatars.discourse-cdn.com/v4/letter/a/c5a1d2/32.png) [@aaelghat](https://boards.straightdope.com/u/aaelghat)\
**Post date:** [November 4, 2011, 1:25pm UTC](https://boards.straightdope.com/t/linux-bash-shell-question/601740/3 "2011-11-04T13:25:29Z")

</div>

Thank you, Sizzles - that did the trick!!! You were extremely helpful. Thanks, again!
