Duke-UNC Brain Imaging and Analysis Center
BIAC Forums | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password   Forgot your Password?
 All Forums
 Support Forums
 Unix Support
 batching renaming files
 New Topic  Reply to Topic
 Printer Friendly
Author  Topic Next Topic  

ch186
New Member

20 Posts

Posted - Jan 17 2014 :  10:14:49 AM  Show Profile  Reply with Quote
Hi-

I am trying to rename my files by deleting the dates out of the middle of the names. I have been trying to "rename" command and looking at the documentation online but haven't figured it out yet. Anyone know the easiest way to do this?

I have a bunch of files named FOO_DATE_SCANNUM_FA.nii.gz and want to get the date deleted out of the name. The date is in the exact space for each file (characters 4-12).

syam.gadde
BIAC Staff

USA
419 Posts

Posted - Jan 17 2014 :  10:35:25 AM  Show Profile  Reply with Quote
'rename' probably won't be as useful, as you need to know the actual string you want to replace, and if you know that already, you might as well use 'mv'.

You can try this, which checks for 8 numbers in the second underscore-separated field and removes it:

for filename in *.nii.gz ; do
newfilename=`echo "${filename}" | sed -e 's/^\([^_]*\)_[0-9]*_/\1_/g'`
echo mv "${filename}" "${newfilename}"
done

Another option is:

for filename in *.nii.gz ; do
newfilename=`echo "${filename}" | cut --complement -d _ -f 2`;
echo mv "${filename}" "${newfilename}";
done

This one is not as safe, as it might remove things that aren't actually 8 numbers (surrounded by underscores).

For safety, both of the examples above just print out the commands. If they look OK, you can get rid of the "echo" in the "echo mv" commands, and it will actually do the renaming. Be careful!
Go to Top of Page

ch186
New Member

20 Posts

Posted - Jan 17 2014 :  10:40:27 AM  Show Profile  Reply with Quote
Thanks Syam, worked perfectly. I tried "sed" at first but always get confused when the sed gets that complicated :)
Go to Top of Page
   Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
BIAC Forums © 2000-2010 Brain Imaging and Analysis Center Go To Top Of Page
This page was generated in 0.05 seconds. Snitz Forums 2000