LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   mv each file with a prefix 1 2 3 ....???? (https://www.linuxquestions.org/questions/linux-newbie-8/mv-each-file-with-a-prefix-1-2-3-a-4175449697/)

Drigo 02-11-2013 12:35 PM

mv each file with a prefix 1 2 3 ....????
 
So lets say I have 3 files with very long names and I want to move them using a for loop....(or other options if it works):

#files in $DIREC:
#fasdfalsdfjas;dlkfjasldkfjasdlfkjs.dcm
#fasdfasfaifjre398r3948fjvdsf .dcm
#f430f8uf0vjdsofvdoivjw048jvdfk.dcm


#THIS IS THE CODE I AM THINKING ABOUT...
for FILE in $( ls $DIREC*.dcm ); do
mv $FILE ~/myname.dcm
done
##############


#BUT THIS WILL OVERWRITE EACH FILE...I want my output to look like this:

#OUTPUT:
~/myname1.dcm
~/myname2.dcm
~/myname3.dcm


#THIS IS DIFFICULT BECAUSE:
1. $DIREC could have more than 3 files.
2. names have no contast letters
3. I dont know how to use arrays in bash...

PLEASE HELP ME!

schneidz 02-11-2013 12:40 PM

Code:

i=0;for FILE in $DIREC*.dcm
do
 i=`expr $i + 1`
 mv "$FILE" ~/myname`printf "%05d" $i`.dcm
done


Drigo 02-11-2013 01:15 PM

Could you explain each coding you've done ? Also, could I get not prefix if there is only one single file?

schneidz 02-11-2013 01:18 PM

i put comments in the code below:
Quote:

Originally Posted by schneidz (Post 4889278)
Code:

i=0;for FILE in $DIREC*.dcm # initializes loop counter; creates condition for loop control variable
do
 i=`expr $i + 1` # increments loop counter
 mv "$FILE" ~/myname`printf "%05d" $i`.dcm # moves file zero padding the counter to 5 digits
done


you can probably prevent prefixing (postfixing) by using an if statment.

suicidaleggroll 02-11-2013 01:31 PM

Also note a key difference between the for loop you proposed and the one schneidz used:

Code:

for FILE in $(ls $DIREC*.dcm)
versus
Code:

for FILE in $DIREC*.dcm
While for most files these two approaches are interchangeable, if any of the files have a space in them, the first approach (parsing the output of ls) will break, while the second approach (direct globbing) will not.

David the H. 02-11-2013 01:55 PM

See here for an explanation of the above:

Don't Read Lines With For


Also, there's rarely any need for expr in modern shells. Integer math, at least, is available built-in. In addition, bash's version of printf can directly set a variable.


Code:

i=1
for fname in "$DIREC/"*.dcm; do

    printf -v newname "$HOME/myname%05d.dcm" "$(( i++ ))"
    mv "$fname" "$newname"

done

Finally: $(..) is highly recommended over `..`

Drigo 02-11-2013 01:55 PM

Thanks for the note!
I'll give it a try....specially in the am if statement.

Drigo 02-11-2013 02:01 PM

anyways I can change numbers 1 2 3 4... for letters a b c d...?

David the H. 02-11-2013 02:06 PM

Not directly, since the above is relies on mathematic incrementing. You'd have to do something like set up an array to substitute letters for numbers, or mess with ascii value incrementing or something like that.

But unfortunately I have to sign off now, so I won't be able to post anything for a while. Maybe someone else can post something.


All times are GMT -5. The time now is 09:31 PM.