LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   mv a 2 digit to name....please help (https://www.linuxquestions.org/questions/linux-newbie-8/mv-a-2-digit-to-name-please-help-4175449723/)

Drigo 02-11-2013 05:04 PM

mv a 2 digit to name....please help
 
So lets say I have in $FOLDER:

13432_32_12.nii
12341_35_12.nii
34523_45_12.nii
.
.
.


I want to replace (in this example) "_12.nii" with _MPRAGE.nii. Though in $FOLDER I have other files with _11.nii _5.nii , etc... :/
I am working with hundreds of them....
#########################
So the output should be:


13432_32_MPRAGE.nii
12341_35_MPRAGE.nii
34523_45_MPRAGE.nii
.
.
.
#The other files with other suffixes _10 _5 etc...should not do anything



Here is what I have in mind but I am not good with regular expression so could you please help me?


for FILE in $FOLDER; do
if <some_regular that brings the "12> -eq "_12" ; then
mv $FILE ${FILE/"_12.nii"/"MPRAGE.nii"
fi
done

Kustom42 02-11-2013 05:17 PM

So is all you are wanting to do is replace the _12 with _MPRAGE? If so that would be very simple. Let us know because I think I may be missing something that you are trying to do here.

TobiSGD 02-11-2013 05:24 PM

Which distro are you using? I ask because different distros use different types of the rename command.
For example, on Slackware you simply could do this:
Code:

rename _12. _MPRAGE. *.nii
On other distributions other syntax may be used, so have a look at the man-page for your rename command.

allend 02-11-2013 05:28 PM

An easy way from a GUI, if you have the ThunarBulkRename utility, is to do a Search and Replace on the Name only.

sag47 02-11-2013 05:38 PM

This would do what you want...
Code:

find . -type f -name '*_12.nii' | while read line;do mv "${line}" "${line%_12.nii}_MPRAGE.nii";done
However I'm confused about one thing. You say there are other files ending with _5.nii and _11.nii. Do you want those to be renamed as well? If you do want those to be renamed then how would you handle the case of the following two files?

Code:

13432_32_12.nii
13432_32_11.nii

If you renamed both of them to end with _MPRAGE.nii then one of the files would be overwritten by the other.

SAM

Kustom42 02-11-2013 05:49 PM

Nobody thought about the overwrite, excellent point sag47! I was waiting for the OP to repost with some more info before I jumped into giving code, some people are just copy/paste crazy and I have helped people break systems before with commands provided based upon incomplete or wrong info.

If you are going to use the mv command you can use the -i option so you are prompted in the event there would be any file name conflicts.

Drigo 02-11-2013 11:17 PM

Nope, just replace the _12.nii files and dont do anything with the other suffixes or replaces let say teh _5.nii with DTI.nii ... I think it look pretty simple with the rename command...the problem is if I have...

13432_12_12.nii
12341_35_12.nii
34523_45_12.nii
.
.
.

Wanting...
#########################

13432_12_MPRAGE.nii
12341_35_MPRAGE.nii
34523_45_MPRAGE.nii

instead of :

13432_MPRAGE_MPRAGE.nii
12341_35_MPRAGE.nii
34523_45_MPRAGE.nii

TobiSGD 02-11-2013 11:26 PM

There should be no problem if you use the program like I described. it will not replace any occurrence of 12, it will only replace occurrences of _12. (including the dot at the end).

sag47 02-11-2013 11:41 PM

Both TobiSGD and my examples work. If you want to learn more about my example then look at the bash man page ("man bash" in terminal) and search for the section parameter expansion. Also, read the rename man page ("man rename") to understand TobSGD's command better.


All times are GMT -5. The time now is 08:52 AM.