king vash
August 31st, 2008, 10:54 PM
I am writing a prefix adder script. Given a directory and a prefix it scans the directory looking for files without the prefix and add it. I am having some trouble scanning the folder. I know that eventually I will encounter a file or folder with spaces so I did not use my normal
for name in $(find *)
do
echo $name
code to check and add prefix
done
I instead tried this.
find "$1" -name "*" -type f| while read name
do
echo $name
code to check and add prefix
done
My code to prefix is
if [ {name:0:$prefixlengh} = $2 ]
then
echo "had prefix"
else
echo "$name"
echo "$prefix""$name"
mv $name "$prefix""$name"
fi
which works great if the file is formatted as "name.ext" but fails when the file is formatted as "./directory/to/file/name.ext"
What can I do about this?
for name in $(find *)
do
echo $name
code to check and add prefix
done
I instead tried this.
find "$1" -name "*" -type f| while read name
do
echo $name
code to check and add prefix
done
My code to prefix is
if [ {name:0:$prefixlengh} = $2 ]
then
echo "had prefix"
else
echo "$name"
echo "$prefix""$name"
mv $name "$prefix""$name"
fi
which works great if the file is formatted as "name.ext" but fails when the file is formatted as "./directory/to/file/name.ext"
What can I do about this?