This should work:
find . -type f -name "*.GEOT14246.*" -print0 | \
xargs -0 rename 's/GEOT14246/GEOT15000/'
given there is not directories named *.GEOT14246.*
A bash variant using find could be something like:
while IFS= read -r -d $'\0' file; do
printf "MV: %-40s => %s\n" "$file" "${file/GEOT14246/GEOT15000}"
mv "$file" "${file/GEOT14246/GEOT15000}"
done < <(find . -type f -name "*.GEOT14246.*" -print0)
| |
+--- this is starting directory +--- This ensures no hiccup
if newline etc. in name
Relative, but full, paths are passed from find – which you should see from the
printf
statement.
The new name is compiled by using bash:
${some_variable/find/replace}
to replace all find
's use:
${some_variable//find/replace}
etc. More here. This could also be a good read: BashPitfalls, UsingFind.
Read some guides like BashGuide. Find some tutorials on-line, but usually never trust them, ask here or on irc.freenode.net #bash.
You do not need to invoke the script by calling sh
. That would also call Bourne shell (sh) and not Bourne Again shell (bash). If you intend to run it with bash; issue bash file
. .sh
extension is also out of place.
What you normally do is make the file executable by:
chmod +x script_file
and then run it with:
./script_file
The shebang takes care of what environment the script should run in.
In your script you do not use the passed path-name anywhere. The script has
a "argument list" starting from $0
which is the script name, $1
first argument, $2
second, - and so on.
In your script you would do something in the direction of:
# Check if argument 1 is a directory, if not exit with error
if ! [[ -d "$1" ]]; then
printf "Usage: %s [PATH]\n" "$(basename "$0")" >&2
exit 1
fi
# OK
path="$1"
while ...
done < <(find "$path" -type f ...)
Your current mv
statement would move all files to wherever you issue the command – to one file named .GEOT14246. (as in overwrite for each mv
statement):
Before script run:
$ tree
.
└── d1
├── a1
│ ├── a2
│ │ ├── a3
│ │ │ └── foo.GEOT14246.faa
│ │ └── foo.GEOT14246.faa
│ └── foo.GEOT14246.faa
└── b1
├── b2
│ ├── b3
│ │ └── foo.GEOT14246.faa
│ └── foo.GEOT14246.faa
└── foo.GEOT14246.faa
After script run:
$ tree
.
├── d1
│ ├── a1
│ │ └── a2
│ │ └── a3
│ └── b1
│ └── b2
│ └── b3
└── *.GEOT15000.*
Also files / paths with spaces or other funny things would make the script go bad and spread havoc. Therefor you have to quote your variables (such as "$file"
).