Add part of pathname/subfolder name to beginning of filename

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Add part of pathname/subfolder name to beginning of filename
# 1  
Old 07-06-2016
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
Code:
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'
Code:
(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:

Code:
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
Code:
 '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?
# 2  
Old 07-06-2016
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.
# 3  
Old 07-06-2016
If I recall correctly, Ubuntu 12.4 and bash were mentioned in an earlier thread... so, try
Code:
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?
# 4  
Old 07-06-2016
Quote:
Originally Posted by RudiC
If I recall correctly, Ubuntu 12.4 and bash were mentioned in an earlier thread... so, try
Code:
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.

Code:
├── 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

# 5  
Old 07-06-2016
Did you try it? It will echo only, i.e. just propose what it would do...
# 6  
Old 07-06-2016
Quote:
Originally Posted by RudiC
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?
# 7  
Old 07-07-2016
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add part of directory name to filename

Hello, I need to add a part of folder name to the files inside it. For instance the file is HMCBackup_20150430.155027.tgz and it is under directory /nim/dr/HMCBackup/cops22 I need to add cops22 to the file name so as it would be cops22_HMCBackup_20150430.155027.tgz Any help in doing... (10 Replies)
Discussion started by: hasn318
10 Replies

2. UNIX for Dummies Questions & Answers

Renaming files with part of their pathname and copying them to new directory

Hi I think this should be relatively simple but I can't figure it out. I have several files with the same name in different folders within a directory (the output of a program that I ran). Something like this: ./myAnalysis/item1/round1/myoutput.txt ./myAnalysis/item1/round2/myoutput.txt... (2 Replies)
Discussion started by: jullee
2 Replies

3. UNIX for Dummies Questions & Answers

Replacing part of filename

Hi guys! I have quite a lot of files like all_10001_ct1212307460308.alf* and I want to get rid of the first number for all at once like: all_ct1212307460308.alf* How can I do this in the shell? (12 Replies)
Discussion started by: TimmyTiz
12 Replies

4. UNIX for Dummies Questions & Answers

[Solved] Appending beginning of filename to end

Hi Guys, I have serveral directories like this: (2013) blablabla(blabla) - blabla (blabla) or (1997) blablabla(blabla) - blabla (blabla) and have to rename them to something like that: blablabla(blabla) - blabla (blabla) (2013) and blablabla(blabla) - blabla (blabla) (1997) Easy... (2 Replies)
Discussion started by: Nateshift
2 Replies

5. Programming

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My code: if then set "subscriber" "promplan" "mapping" "dedicatedaccount" "faflistSub" "faflistAcc" "accumulator"\ "pam_account"; for i in 1 2 3 4 5 6 7 8;... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

6. UNIX for Dummies Questions & Answers

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My Requirement: 1) There are some set of files in a directory like given below OTP_UFSC_20120530000000_acc.csv OTP_UFSC_20120530000000_faf.csv OTP_UFSC_20120530000000_prom.csv... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

7. Shell Programming and Scripting

Getting part of a filename

Hi All, I'm trying to get part of a filename and my skill with regular expression are lacking. I know I need to use SED but have no idea how to use it. I'm hoping that someone can help me out. The file names would be: prefix<partwewant>suffix.extension the prefix and suffix are always 3... (4 Replies)
Discussion started by: imonkey
4 Replies

8. Shell Programming and Scripting

Link multiple files from different subfolder to a new subfolder

Hi, I have the following subfolder with files: /data/a/1/xxx.txt /data/b/2/yyy.txt /data/c/3/zzz.txt And i have a set of new folders which have exactly the same structure as above but different disk without the files: /data_02/a/1/ /data_02/b/2/ /data_02/c/3/ Now i would like to... (6 Replies)
Discussion started by: total_ysf
6 Replies

9. Shell Programming and Scripting

script to include filename with variations at the beginning of the file

Hi there, I have a bunch of files that I want to modify. As a beginner, but nevertheless enthusiast, in the use of the shell I want to create a script enabling me to modify those files quickly. I had only some partial success with various peaces of scripts but I would like to create one script... (1 Reply)
Discussion started by: iamzesh
1 Replies

10. Shell Programming and Scripting

part of a filename

Hi, I need to extract only a part of the filenames of some files. The files are named this way : .tap_profile_SIT02 I want the "SIT02" part, which is not the same for each file. I was able to get what I want with bash, but not with ksh. Here is the command I used in bash : find... (8 Replies)
Discussion started by: flame_eagle
8 Replies
Login or Register to Ask a Question