0

I am trying to remove one character from multiple directory names but I am unsure on how to set up a loop for this.

Part to remove is '1' (the last digit in the directory name)
e.g. S123451/
     S234561/
     S345671/

Also, I am trying to remove portion of a file name from multiple files in subfolders but I am unsure on how to set up a loop for this.

Part to remove '_*_*_*****' (everything after the 1st underscore up to before the file extension)

e.g  S123451/
        /9/
          S12345_1_9_00001.nii.gz
        /4/
          S12345_1_4_00001.nii.gz
          S12345_1_4_00001.bval
          S12345_1_4_00001.bvec
 S234561/
        /2/
          S23456_1_2_00001.nii.gz
        /1/
          S23456_1_1_00001.nii.gz
          S23456_1_1_00001.bval
          S23456_1_1_00001.bvec
 S345671/
        /5/
          S34567_1_5_00001.nii.gz
        /20/
          S34567_1_20_00001.nii.gz
          S34567_1_20_00001.bval
          S34567_1_20_00001.bvec

1 Answer 1

0

With Larry Wall's rename (Debian/ubuntu) or prename (Fedora/RHEL/CentOS):

Directories:

rename -n 's/.$//' *

in other words, replace the last character by nothing. -n is for dry runs, remove or replace by -v for actual execution. If you use the */ pattern to make sure you list only directories:

rename -n 's:./$::' */

Files:

rename -n 's/_[^.]+//' *

in other words, replace the first underscore and everything that follows that is not a dot by nothing.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .