You are not logged in.

#1 2009-05-12 19:38:39

robmaloy
Member
From: Germany
Registered: 2008-05-14
Posts: 263

[SOLVED] bash recursive function

#!/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

#2 2009-05-12 20:18:53

Peasantoid
Member
Registered: 2009-04-26
Posts: 928
Website

Re: [SOLVED] bash recursive function

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

#3 2009-05-12 20:38:13

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED] bash recursive function

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

#4 2009-05-12 22:15:23

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [SOLVED] bash recursive function

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 smile

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)

Offline

#5 2009-05-12 23:01:59

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED] bash recursive function

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

#6 2009-05-12 23:50:16

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,796
Website

Re: [SOLVED] bash recursive function

Procyon wrote:
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)

Offline

#7 2009-05-13 18:10:27

robmaloy
Member
From: Germany
Registered: 2008-05-14
Posts: 263

Re: [SOLVED] bash recursive function

brisbin33 wrote:

why would you test -f $file after a find?

$file is the new filename


thx procyon, the subshell thing worked!


☃ Snowman ☃

Offline

Board footer

Powered by FluxBB