Add part of pathname/subfolder name to beginning of filename

Hello everyone,
I need some help renaming files. I have a directory with a bunch of subfolders in it like so with DWI being the working directory and the 3 levels after it being subdirectories

home/user/Documents/DWI/111180-100/3t_2016-01-07_21-42/003_DTI_siemens_TClessdistort/dtifit_FA.nii.gz
home/user/Documents/DWI/111445-100/3t_2016-01-07_21-42/003_DTI_siemens_TClessdistort/dtifit_FA.nii.gz
etc

Those subfolders all have a couple of files that begin with 'dti' and end in 'nii.gz'

(ex: dti_FA.nii.gz, dti_V1.nii.gz etc)

I need a script that will loop over all the subfolders and rename the file so that it adds the bolded part of the pathname to the beginning of the file. Example:

home/user/Documents/DWI/111180-100/3t_2016-01-07_21-42/003_DTI_siemens_TClessdistort/dtifit_FA.nii.gz
home/user/Documents/DWI/111445-100/3t_2016-01-07_21-42/003_DTI_siemens_TClessdistort/dtifit_FA.nii.gz

to rename the file in each subfolder from

 'dti_FA/V1etc.nii.gz' to 'specifiedstringfrompath_dti_*.nii.gz' or '111180-100_dti_FA/V1etc.nii.gz'

I know I need to use the for loop and perhaps globbing but I don't know how to go about adding that string from the subdirectory to the file?

We can't really begin to help until we know your:-

1) Operating System.
2) Which shell is allowed.
3) What machine(s) it is to be run on.
Etc, etc...

We can assume 'bash' but it could just as easily be 'csh' as both are so radically different as to require almost totally different approaches.

If I recall correctly, Ubuntu 12.4 and bash were mentioned in an earlier thread... so, try

find home/user/Documents/DWI -type f -name "dti*nii.gz" | while read FN; do TMP=${FN#*/*/*/*/}; TMP=${TMP%%/*}; echo mv $FN ${FN/dti/${TMP}_dti}; done 
mv home/user/Documents/DWI/111180-100/3t_2016-01-07_21-42/003_DTI_siemens_TClessdistort/dtifit_FA.nii.gz home/user/Documents/DWI/111180-100/3t_2016-01-07_21-42/003_DTI_siemens_TClessdistort/111180-100_dtifit_FA.nii.gz
mv home/user/Documents/DWI/111445-100/3t_2016-01-07_21-42/003_DTI_siemens_TClessdistort/dtifit_FA.nii.gz home/user/Documents/DWI/111445-100/3t_2016-01-07_21-42/003_DTI_siemens_TClessdistort/111445-100_dtifit_FA.nii.gz

The echo is in there to stay on the safe side; remove it when you're happy with the "proposed" result.
Am I right in guessing the /V1etc in your sample results is an undesired artefact?

Yes, I am working with bash, ubuntu 12.4.

Will this code rename the files in a batch. I need to rename all the files, inside all subfolders, that begin with "dti". Is there a way I can do the same with a for loop or shopt glob?

Here is a sample of the directory structure. There are many more subfolders but I've only shown 3 for the purposes of keeping it short.

 111180-100
**  3t_2016-01-07_21-42
**      003_DTI_siemens_TClessdistort
**          0001.dcm
**          111180-100_eddy_corrected_brain_mask.nii.gz
**          111180-100_eddy_corrected_brain.nii.gz
**          111180-100_eddy_corrected.ecclog
**          111180-100_eddy_corrected.nii.gz
**          20160107_214213DTIsiemensTClessdistorts003a001.bval
**          20160107_214213DTIsiemensTClessdistorts003a001.bvec
**          20160107_214213DTIsiemensTClessdistorts003a001.nii.gz
**          dti_FA.nii.gz
**          dti_L1.nii.gz
**          dti_L2.nii.gz
**          dti_L3.nii.gz
**          dti_MD.nii.gz
**          dti_MO.nii.gz
**          dti_S0.nii.gz
**          dti_V1.nii.gz
**          dti_V2.nii.gz
**          dti_V3.nii.gz
 111405-100
**  3t_2015-12-08_21-54
**      003_DTI_siemens_TClessdistort
**          0001.dcm
**          111405-100_eddy_corrected_brain_mask.nii.gz
**          111405-100_eddy_corrected_brain.nii.gz
**          111405-100_eddy_corrected.ecclog
**          111405-100_eddy_corrected.nii.gz
**          20151208_215447DTIsiemensTClessdistorts003a001.bval
**          20151208_215447DTIsiemensTClessdistorts003a001.bvec
**          20151208_215447DTIsiemensTClessdistorts003a001.nii.gz
**          dti_FA.nii.gz
**          dti_L1.nii.gz
**          dti_L2.nii.gz
**          dti_L3.nii.gz
**          dti_MD.nii.gz
**          dti_MO.nii.gz
**          dti_S0.nii.gz
**          dti_V1.nii.gz
**          dti_V2.nii.gz
**          dti_V3.nii.gz
 111440-100
**  3t_2016-01-27_20-58
**      003_DTI_siemens_TClessdistort
**          0001.dcm
**          111440-100_eddy_corrected_brain_mask.nii.gz
**          111440-100_eddy_corrected_brain.nii.gz
**          111440-100_eddy_corrected.ecclog
**          111440-100_eddy_corrected.nii.gz
**          20160127_205836DTIsiemensTClessdistorts003a001.bval
**          20160127_205836DTIsiemensTClessdistorts003a001.bvec
**          20160127_205836DTIsiemensTClessdistorts003a001.nii.gz
**          dti_FA.nii.gz
**          dti_L1.nii.gz
**          dti_L2.nii.gz
**          dti_L3.nii.gz
**          dti_MD.nii.gz
**          dti_MO.nii.gz
**          dti_S0.nii.gz
**          dti_V1.nii.gz
**          dti_V2.nii.gz
**          dti_V3.nii.gz
 example

Did you try it? It will echo only, i.e. just propose what it would do...

Okay, it worked. I just had to add a few more asterisks. Could you explain what the code/syntax does? Where do I learn all these?

Why and where did you add asterisks? Above should find exactly what you asked for in post#1. To learn all this, you need to code, again and again, read documentation (like man find ), accept and interpret error messages, optimize your code. We're here to help you doing that.

I had to add extra asterisks because when I ran the original code, it added 'DWI_dti_fa.nii.gz' instead of '111180_dti_fa.nii.gz'. I'm guessing the extra asteriskts told it go one folder further. How does that part of the code work? Is it recursively looking into folders..I ask because I've also seen it done using just ** with globbing so I was just wondering what the difference was and why it works. Actually, if you could explain the code, what TMP, FN, %% etc all do/stand for that would be super helpful so I could learn.

I was a bit sceptical when I saw your file name samples like home/user/... guessing it should be /home/user... . That's where the "additional" level came from.

find traverses the directory tree starting at the given dir looking for regular files ( -type f ) and all files matching the pattern given, printing the full path to stdout. For further details see man find .
stdout is piped into a shell while loop that reads the full file name into the FN variable. TMP is another temporary shell variable that is assigned from FN using shell's parameter expansion (see e.g. man bash ). # removes shortest matching prefix pattern, %% removes longest matching suffix pattern. /.../... does pattern substitution.