Results 1 to 5 of 5

Thread: Script to move and rename files

  1. #1
    Join Date
    Oct 2011
    Beans
    8

    Script to move and rename files

    Hi,
    I have got files that I would like to move and rename.

    In the directory ~/ab100/fMRI
    I have got the files dti_ab100_ac2.nii and bold_ab100_ac6.nii

    In the directory ~/ed124/fMRI
    I have got the files dti_ed124_ac1.nii and bold_ed124_ac8.nii

    etc...

    I would like to have
    In the directory ~/ab100
    the files dti_ab100.nii and bold_ab100.nii

    In the directory ~/ed124
    I have got the files dti_ed124.nii and bold_ed124.nii


    Any idea of a script for doing this ?
    many thanks !!
    publicjo

  2. #2
    Join Date
    Aug 2012
    Beans
    76

    Re: Script to move and rename files

    Something like this?

    Code:
    cp -r firstDirectory ~/ab100
    cp -r secondDirecory ~/ed124

  3. #3
    Join Date
    Oct 2011
    Beans
    8

    Re: Script to move and rename files

    Thanks for your quick reply.
    The thing is that I do not have only 2 directories like this but >100 so I am looking for a script using something like "for...." to do this automatically
    thanks!

  4. #4
    Join Date
    Apr 2012
    Beans
    106

    Re: Script to move and rename files

    This should do it (untested):

    Code:
    #!/bin/bash
    
    for dir in ab100/fMRI ed124/fMRI; do
            cd ~/$dir
            for i in $(ls); do
                    mv $i $(echo i | sed 's/_ac[[:digit:]]//')
    done
    Last edited by cmont899; October 5th, 2012 at 11:26 PM. Reason: fixed sed command

  5. #5
    Join Date
    Aug 2012
    Beans
    76

    Re: Script to move and rename files

    I think it should be.

    Code:
    #!/bin/bash
    
    for dir in .*/fMRI; do
            cd ~/$dir
            for i in $(ls); do
                    mv $i $(echo i | sed 's/\/fMRI//')
    done
    Last edited by Pletched; October 6th, 2012 at 02:49 PM. Reason: deleted info

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •