rename files based on their respective directory name


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers rename files based on their respective directory name
# 1  
Old 12-18-2011
rename files based on their respective directory name

I have a number of files in directories labeled like this:

/Data/tr[8428-9915]_gray/tr[8428-9915]_DTI/dti_FA.nii.gz
(the brackets here represent a range of number that the files are labeled with)

I need to rename each dti_FA.nii.gz file according to the name of the folder it resides in. For example, the file /Data/tr8430_gray/tr8430_DTI/dti_FA.nii.gz should be named 8430_dti_FA.nii.gz

Very basic stuff i know...Thank you.
# 2  
Old 12-18-2011
Assuming the file names are all the same as you've indicated, this should work:

Code:
#!/usr/bin/env ksh

# big assumption that this find returns only the files you want to move
# test it first!!!
find /Data -name "*gz" | while read x
do
    n="${x%%_*}"      # dig number after tr and before first _
    n="${n#*tr}"
    fn=${x%/*}/${n}_${x##*/}  # add number to build final pathname
    echo "mv $x $fn"    # remove the echo and quotes to actually rename files
done

Always a good idea to run this kind of script and make sure that the commands generated look good to prevent data loss. Also doesn't hurt to back things up first!
# 3  
Old 12-19-2011
this is so great. thank you!

So I copied one entire directory (tr9915_gray) to a separate "test folder" (/Data/DTI_test) to try it out. I ran the script from this test folder and so I thought I should replace the first line:
find /Data -name "*.gz" | while read x
with this:
find ./ -name "*.gz" | while read x
(not sure if I should have done that)

It seemed to work, except the thing is, there are a lot of files that end in .gz in various subdirectories.... and it renamed all the files ending in .gz in the subdirectories of /tr9915_gray and skipped the ones in /tr9915_gray/tr9915_DTI. Here is an example of the last three lines from the output.

mv ./tr9915_gray/MSIT_PPI_MFG_ROI.feat/reg/highres.nii.gz ./tr9915_gray/MSIT_PPI_MFG_ROI.feat/reg/9915_highres.nii.gz
mv ./tr9915_gray/MSIT_PPI_MFG_ROI.feat/reg/highres2standard.nii.gz ./tr9915_gray/MSIT_PPI_MFG_ROI.feat/reg/9915_highres2standard.nii.gz
mv ./8531_dti_FA.nii.gz ././8531_8531_dti_FA.nii.gz


Not really sure what happened with the last line at all!
Since I just want to rename the file called dti_FA.nii.gz which resides in /tr9915_gray/tr9915_DTI

...could I replace the first line you made:
find ./ -name "*.gz" | while read x

with this??
find ./ -name "*_FA.gz" | while read x

thank you again so much!!
# 4  
Old 12-19-2011
Quote:
Originally Posted by tk0034
this is so great. thank you!

So I copied one entire directory (tr9915_gray) to a separate "test folder" (/Data/DTI_test) to try it out. I ran the script from this test folder and so I thought I should replace the first line:
find /Data -name "*.gz" | while read x
with this:
find ./ -name "*.gz" | while read x
(not sure if I should have done that)
Good call -- both creating the test environment and the change to ./...

Quote:

It seemed to work, except the thing is, there are a lot of files that end in .gz in various subdirectories.... and it renamed all the files ending in .gz in the subdirectories of /tr9915_gray and skipped the ones in /tr9915_gray/tr9915_DTI. Here is an example of the last three lines from the output.

mv ./tr9915_gray/MSIT_PPI_MFG_ROI.feat/reg/highres.nii.gz ./tr9915_gray/MSIT_PPI_MFG_ROI.feat/reg/9915_highres.nii.gz
mv ./tr9915_gray/MSIT_PPI_MFG_ROI.feat/reg/highres2standard.nii.gz ./tr9915_gray/MSIT_PPI_MFG_ROI.feat/reg/9915_highres2standard.nii.gz
mv ./8531_dti_FA.nii.gz ././8531_8531_dti_FA.nii.gz


Not really sure what happened with the last line at all!
Since I just want to rename the file called dti_FA.nii.gz which resides in /tr9915_gray/tr9915_DTI
Is that file just an oddly named file? The script depends on trNNNN so if the tr is missing odd results will happen.

Quote:
...could I replace the first line you made:
find ./ -name "*.gz" | while read x

with this??
find ./ -name "*_FA.gz" | while read x
Yep, that will work too. You can run the find command by itself to see what files it finds and thus what files will be candidates for renaming. You can tweek your find to get just the right things and then let its output go into the read. Seems like you are on the right track.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Rename files based on simple text file

Hello! New here although not completely new to Unix. I wonder how I could rename files based on the data found in a simple textfile. It goes like this: I have 4 files 1 ldfgkkfjslkdfjsldkfjsf.wav 2 nndsdflksdjf.wav 3 sdflksjdf jjsdflsdfl.wav 4 dkadsdddd.wav Textfile.txt looks like... (14 Replies)
Discussion started by: Oortone
14 Replies

2. Shell Programming and Scripting

Rename files based on name in text file

Hello, I have a text file "file.list" with the contents below. file1 filename1 file2 filename2 file3 filename3 file1, file2 and file3 are files existing in the same directory as the text file file.list. I want to rename file1 to filename1, file2 to filename2, as show in the text... (1 Reply)
Discussion started by: james2009
1 Replies

3. UNIX for Dummies Questions & Answers

Rename files based on a list

Hi, I have a directory with a lot of files like this: a.bam b.bam c.bam I like to rename these files based on a list where the name of the files in the first column will be replasced by the names in the second column. Here is my list which is a tab-delimited text file: a x b y c ... (4 Replies)
Discussion started by: a_bahreini
4 Replies

4. Shell Programming and Scripting

Rename Files in remote directory

Hi All, I am creating a script which will connect to remote server with ssh and perfom below. - Search in a directory - Search for a pattern - Check the size if greater than the size_limit rename the file. Below is the code i have written. ssh user@host <<EOF find /tmp... (5 Replies)
Discussion started by: Girish19
5 Replies

5. UNIX for Dummies Questions & Answers

Rename files in a directory and move them

I have a directory e2e_ms_xfer/cent01 this contains the multiple files some of which will be named below with unique date time stamps e2e_ms_edd_nom_CCYYMMDD_HHMM.csv What I want to do is in a loop 1) Get the oldest file 2) Rename 3) Move it up one level from e2e_ms_xfer/cent01 to... (1 Reply)
Discussion started by: andymay
1 Replies

6. Shell Programming and Scripting

How to Rename List of files in a directory

How can i rename list of files in a directory? (4 Replies)
Discussion started by: knip
4 Replies

7. UNIX for Dummies Questions & Answers

Bash script to rename files in a directory

Dear friends, I have created a script to rename all files in a directory by appending the file name with username (who created the file), the date it was created. For example, "apple.doc" should be renamed to "johnFeb23apple.doc" where "john" is the owner and "Feb23" is file created date. It... (4 Replies)
Discussion started by: djsnifer
4 Replies

8. Shell Programming and Scripting

Rename many files in a directory

Hi, I have around 100 xml file in a directory. I need to rename the files from .xml to .xml1. So i tried using the following command: mv *.xml *.xml1 but i am getting the following error mv: when moving multiple files, last argument must be a directory Try `mv --help' for more... (8 Replies)
Discussion started by: ananthi_ku
8 Replies

9. Shell Programming and Scripting

rename files Ax based on strings found in files Bx

Hi, I'm not very experienced in shell scripting and that's probably why I came across the following problem: I do have several hundred pairs of text files (PF00x.spl and PF00x.shd) where the first file (PF00x.spl) needs to be renamed according a string that is included in the second file... (12 Replies)
Discussion started by: inCH
12 Replies

10. Shell Programming and Scripting

Rename files/directories based on their name

i have hundreds of directories that have to be renamed. the directory structure is fairly uniform which makes the scripting a little simpler. suppose i have many directories like this */*/*/*abc* (in other words i have similar directory names 3 dirs deep that all contain the pattern abc in... (8 Replies)
Discussion started by: quantumechanix
8 Replies
Login or Register to Ask a Question