LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell Script to copy and number files (https://www.linuxquestions.org/questions/programming-9/shell-script-to-copy-and-number-files-690048/)

extasic 12-12-2008 05:32 AM

Shell Script to copy and number files
 
Hi!

I need to copy several files from a directory tree. Inside this tree there are several files with the same name, and I have to copy them altogether to the same directory.

This is an example of how it looks

\tmp\a
\tmp\a\1.txt
\tmp\a\2.txt
\tmp\a\3.txt
\tmp\b
\tmp\b\1.txt
\tmp\b\3.txt
\tmp\b\4.txt
\tmp\c
\tmp\c\5.txt
\tmp\c\2.txt

Now I want to copy the files to \tmp\dest1\ as

1.txt, 2.txt .. 8.txt - numbered no matter how they are originally named. I just want to keep the original file extension (that may differ).

How do I realize that?

Thank you in advance!

jcookeman 12-12-2008 06:29 AM

If you absolutely don't care about the original file name, then you can do something like this:

Code:

declare -i I=1
find $FILEPATH -regex .*/[0-9]+\.txt -print | while read FILE
do
    mv $FILE $NEWPATH/$I.txt
    let I=$I+1
done



All times are GMT -5. The time now is 10:54 PM.