LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-11-2013, 01:35 PM   #1
Drigo
Member
 
Registered: Jun 2009
Posts: 111

Rep: Reputation: 17
Angry 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!
 
Old 02-11-2013, 01:40 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,026

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

Last edited by schneidz; 02-11-2013 at 01:47 PM.
 
1 members found this post helpful.
Old 02-11-2013, 02:15 PM   #3
Drigo
Member
 
Registered: Jun 2009
Posts: 111

Original Poster
Rep: Reputation: 17
Could you explain each coding you've done ? Also, could I get not prefix if there is only one single file?
 
Old 02-11-2013, 02:18 PM   #4
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,026

Rep: Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845Reputation: 845
i put comments in the code below:
Quote:
Originally Posted by schneidz View Post
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.

Last edited by schneidz; 02-11-2013 at 04:15 PM.
 
Old 02-11-2013, 02:31 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,258

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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.
 
1 members found this post helpful.
Old 02-11-2013, 02:55 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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 `..`
 
1 members found this post helpful.
Old 02-11-2013, 02:55 PM   #7
Drigo
Member
 
Registered: Jun 2009
Posts: 111

Original Poster
Rep: Reputation: 17
Thanks for the note!
I'll give it a try....specially in the am if statement.
 
Old 02-11-2013, 03:01 PM   #8
Drigo
Member
 
Registered: Jun 2009
Posts: 111

Original Poster
Rep: Reputation: 17
anyways I can change numbers 1 2 3 4... for letters a b c d...?
 
Old 02-11-2013, 03:06 PM   #9
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
split a gzip file based on size and have a prefix without changing extension gubbu Linux - Newbie 3 01-18-2010 06:09 PM
Any tool to highlight lines with certain prefix in a file? n179911 Linux - Software 1 07-06-2009 05:08 AM
difference b/w AF_ prefix and PF_ prefix related to socket Ashok_mittal Linux - Newbie 1 03-20-2008 05:15 PM
how to extract paragraphs from file in BASH script followed by prefix ! , !! and !!! nabmufti Programming 4 02-10-2008 10:23 AM
redirect stderr to file with a prefix? JPMallory Programming 2 12-26-2007 04:10 PM


All times are GMT -5. The time now is 05:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration