Find the answer to your Linux question:
Results 1 to 6 of 6
Hi I want to modify a recurring string section in all file names under the same older. e.g. $HOME/Desktop/Data/ .. center_PR_2_0.4.dat data_PR_2_0.4.dat center_PR_2_0.6.dat data_PR_2_0.6.dat ... to .. center_PRO_2_0.4.dat data_PRO_2_0.4.dat center_PRO_2_0.6.dat ...
  1. #1
    Just Joined!
    Join Date
    Nov 2008
    Posts
    3

    modify filename string section

    Hi

    I want to modify a recurring string section in all file names under the same older.

    e.g. $HOME/Desktop/Data/
    ..
    center_PR_2_0.4.dat
    data_PR_2_0.4.dat

    center_PR_2_0.6.dat
    data_PR_2_0.6.dat
    ...

    to


    ..
    center_PRO_2_0.4.dat
    data_PRO_2_0.4.dat

    center_PRO_2_0.6.dat
    data_PRO_2_0.6.dat
    ...
    Not too sure where to start with a shell script.

    SHuo

  2. #2
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    Try this script first
    Code:
    #!/bin/bash
    DIR=~/Desktop/data
    find $DIR -name "*PR*dat"| while read f; do
      NEWNAME=`dirname $f`/`basename "$f" | sed 's/PR/PRO/'`
      echo mv "$f" "$NEWNAME"
    done
    It it shows exactly what you want, remove the echo from the script.

  3. #3
    Just Joined!
    Join Date
    Nov 2008
    Posts
    3
    I applied the script and it replaces files in a random manner - at least on the print screen. However I checked the folder content and the file names haven't changed at all.

  4. #4
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    echo in my previous post is just to show you what action will be taken. Now try this one.
    Code:
    #!/bin/bash
    DIR=~/Desktop/data
    find $DIR -name "*PR*dat"| while read f; do
      NEWNAME=`dirname $f`/`basename "$f" | sed 's/PR/PRO/'`
      mv "$f" "$NEWNAME"
    done

  5. #5
    Just Joined!
    Join Date
    Nov 2008
    Posts
    3
    Thanks, now its working. Hoe can I print to the screen while it's modifying the filenames by the way?

  6. #6
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    #!/bin/bash
    DIR=~/Desktop/data
    find $DIR -name "*PR*dat"| while read f; do
    NEWNAME=`dirname $f`/`basename "$f" | sed 's/PR/PRO/'`
    echo renaming "$f" to "$NEWNAME"
    mv "$f" "$NEWNAME"
    done

Posting Permissions

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