You are not logged in.
Pages: 1
#!/bin/bash
IFS=$'\n'
function low {
if [ -d "$1" ];
then
DIR="$1"
cd "$DIR"
fi
pwd
for f in `find . -maxdepth 1`; do
[ -d "$f" -a "$f" != "." ] && low "$f"
file=$(echo $f | tr A-Z a-z | tr ' ' '_')
[ -e "$f" ] && [ ! -e "$file" ] && mv "$f" "$file"
done
[ "$DIR" ] && cd - > /dev/null
}
low .
unset IFS
this should walk through all subdirectories and rename the files inside there
however, it only goes through the first directories it finds.
i.e. i have:
/tmp/test1/
/tmp/test1/sub1/
/tmp/test2/
/tmp/test2/sub2/
and the script does:
[rob:/tmp/] ./lowandspace
/tmp/
/tmp/test2
/tmp/test2/test2_2
what am i missing?
or is bash screwing up?
Last edited by robmaloy (2009-05-20 09:46:54)
☃ Snowman ☃
Offline
I realize this isn't a solution to your problem, but shouldn't the following code work?
LFS=$'\n'
find -maxdepth 1 | while read filename; do
if [ -f "$filename" ]; then
filedir="$(dirname $filename)"
newname="$(basename $filename | tr A-Z a-z | tr ' ' '_')"
mv "$filename" "$filedir/$newname"
fi
done
I whipped this up in about thirty seconds, so there are probably a few errors. (Modify as necessary.)
# Also, it renames files in the current directory rather than its subdirectories, but you get the idea...?
# Okay never mind, this code fails badly. Nothing to see here.
Last edited by Peasantoid (2009-05-12 20:25:14)
Offline
A function call is not a subshell. When second item in the for loop is up you are still in the directory you reached with the depth traversal.
So call the function in a subshell:
[ -d "$f" -a "$f" != "." ] && (low "$f")
Offline
why would you test -f $file after a find?
this should work, i use something very similar except mine renames directories too so it's a bit easier to manage. i overused the double quotes b/c i can't test right now and i want to be sure it handles spaces appropriately
find ./ -type f | while read file do
dir="$(dirname "$file")"
old="$(basename "$file")"
new="$(echo "$old" | tr A-Z a-z | tr ' ' '_')"
if [ "$new" != "$old" ]; then
mv "$dir"/"$old" "$dir"/"$new"
echo "$old was renamed to $new"
fi
done
Last edited by brisbin33 (2009-05-12 22:17:28)
//github/
Offline
find -type f -exec bash -c 'mv -vn '\''{}'\'' "$(echo '\''{}'\'' | tr " " _ | tr A-Z a-z)"' \;
Only gives some extra mv: x and x are the same file output
Offline
find -type f -exec bash -c 'mv -vn '\''{}'\'' "$(echo '\''{}'\'' | tr " " _ | tr A-Z a-z)"' \;
Only gives some extra mv: x and x are the same file output
damn you procyon, always trumping my nice bash scripts with find one liners!
edit: i think i gave you too much credit, won't that fail if there are any spaces or uppercase in directory or sub directory names?
Last edited by brisbin33 (2009-05-13 12:44:57)
//github/
Offline
why would you test -f $file after a find?
$file is the new filename
thx procyon, the subshell thing worked!
☃ Snowman ☃
Offline
Pages: 1