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
...
- 11-15-2008 #1Just 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
- 11-15-2008 #2Linux User
- Join Date
- May 2008
- Location
- NYC, moved from KS & MO
- Posts
- 251
Try this script first
It it shows exactly what you want, remove the echo from the script.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
- 11-15-2008 #3Just 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.
- 11-15-2008 #4Linux 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
- 11-15-2008 #5Just 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?
- 11-15-2008 #6Linux 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


Reply With Quote