You are not logged in.

#1 2009-06-03 10:51:51

whoops
Member
Registered: 2009-03-19
Posts: 891

[solved] use WHAT to rename stuff? (not rename [util-linux-ng 2.14.2])

Hi!

I can't figure out rename... is this a different version from the one I'm used to or did I forget how to use it?

- the -n command does not seem to work, nor is it mentioned anywhere. Same for -v
- "man rename" does not mention anything about regex or -v / -n
- I'm getting crazy. Been trying to rename a simple regex string inside a folder + subfolders for hours now with find -exec rename and regex and all I can do so far is mess up my testing dummy of said folder(s).

thx

Last edited by whoops (2009-06-03 16:28:03)

Offline

#2 2009-06-03 11:01:19

lucke
Member
From: Poland
Registered: 2004-11-30
Posts: 4,018

Re: [solved] use WHAT to rename stuff? (not rename [util-linux-ng 2.14.2])

I think that's the rename you're looking for: http://aur.archlinux.org/packages.php?ID=21827

Offline

#3 2009-06-03 11:10:18

whoops
Member
Registered: 2009-03-19
Posts: 891

Re: [solved] use WHAT to rename stuff? (not rename [util-linux-ng 2.14.2])

Aaah, looks like it, thanks!

What would be the "arch native alternative" for doing such stuff "adhoc" ("single line", no script...)? Using mv with some regex tool or something?

pseudo:
find . -regex .*test.* -exec mv {} $(regextool 's/from/to/' {}) \;
?

Offline

#4 2009-06-03 11:39:48

lucke
Member
From: Poland
Registered: 2004-11-30
Posts: 4,018

Re: [solved] use WHAT to rename stuff? (not rename [util-linux-ng 2.14.2])

There's a great "zmv" tool in zsh. I'm not really sure if you can easily accomplish it in one line with bash.

Offline

#5 2009-06-03 14:19:54

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

Re: [solved] use WHAT to rename stuff? (not rename [util-linux-ng 2.14.2])

you're very close with find -exec, although i find that it has it's limitations...

find ./ -name *test* -exec mv {} {}.new \;

works well but you don't get the sort of renaming you're looking for

find ./ -name *test* -exec mv {} $(echo {} | sed 's/from/to/g') \;

really _feels_ like it should work, but it doesn't.  i think Procyon knows the proper syntax to do this sort of thing in one find line, but i just cheat:

for file in find ./ -name *test*; do new="$(echo $file | sed 's/from/to/g')"; mv $file $new; done

i've never used zmv (or mmv) but i hear they're nice.  whatever works for you.

Offline

#6 2009-06-03 15:30:18

whoops
Member
Registered: 2009-03-19
Posts: 891

Re: [solved] use WHAT to rename stuff? (not rename [util-linux-ng 2.14.2])

Thx, not exactly what I searched, but that last one did it! And it's still handy enough to use it manually.

Still - I'm trying to understand find/sed/bash/stugg better and I'm curious why sed doesn't "want" to replace... and why somehow find gets the {} instead of sed. Can't figer it out, that's strange:

$ mkdir test.file
$ find ./ -name *test* -exec echo {} $(echo "es{}es" | sed 's/es/aaa/g') \;
./test.file aaa./test.fileaaa

The {} is at the right place, es&es around it is processed correctly... and now:

$ mkdir test.directory
$ mkdir another.test
$ find ./ -name *test* -exec echo {} $(echo "es{}es" | sed 's/es/aaa/g') \;
find: paths must precede expression: test.directory
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]

Straaange... can't wrap my brain around that...

when it finds more than one entry that matches a directory, somehow find gets it (which results in wrong syntax) - then maybe it's the same with finding files? Just there's no error to be seen, because it doesn't match the "wrong syntax"....

Tried different combinations of \escapes in front of special characters and " or ' around some stuff (escaped and nonescaped + multi-escape-cascade-chaos), didn't get it to do anything better.

Last edited by whoops (2009-06-03 15:33:59)

Offline

#7 2009-06-03 15:35:57

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

Re: [solved] use WHAT to rename stuff? (not rename [util-linux-ng 2.14.2])

@brisbin33: yeah it was with find -exec bash -c ' ... '

find -name \*.utf -exec bash -c 'echo "{}" $(echo {} | sed "s/[0-9]/#/g;s/ //g")' \;

Quoting will quickly become a problem and it's probably less efficient. Better stick with the for loop.

Offline

#8 2009-06-03 16:26:11

whoops
Member
Registered: 2009-03-19
Posts: 891

Re: [solved] use WHAT to rename stuff? (not rename [util-linux-ng 2.14.2])

That already gets me a bit further, thx! But somehow, with find in general I'm really producing to many errors... somehow - "bash: command substitution: line 0: syntax error near unexpected token `(' " - and stuff... Might really not be the best tool for me with -exec alone.

With the for loop I keep getting messed up stuff with files that contain spaces, as I don't manage to get those " " around every single file name.

I think I'm going to practice while for a while now (forgot about that all together, until I managed to screw up my "for-loop" stuff once again)... somehow that's more compatible with how me thinking works and it's easier for me to "play" with it:

while read file; do new="$(echo $file | sed 's/from/to/g')"; echo command \"$file\" TO \"$new\"; done < <(find ./ -name '*test*')

=> [solved]

Offline

#9 2009-06-03 16:30:49

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

Re: [solved] use WHAT to rename stuff? (not rename [util-linux-ng 2.14.2])

glad you've got it psuedo-solved.  for reference, here's a script wrote to recursively remove spaces and uppercase in filenames (i.e. it handles spaces etc properly.)

it's a bit complex to be a one liner (but with enough ';'s obviously it could be wink).  anyway, here you go:

#!/bin/bash
# pbrisbin 2009
###

# input error checking
if [ -z "$1" ];then
  echo Give target directory
  exit 1
fi

# run it
find "$1" -depth -name '*' | while read file; do
  dir=$(dirname "$file")
  old=$(basename "$file")
  new=$(echo $old | tr 'A-Z' 'a-z' | tr ' ' '_' | sed s/_-_/_/g | sed s/__/_/g) # adjust this as needed
  if [ "$old" != "$new" ]; then
    mv -i "$dir/$old" "$dir/$new"
    echo "$old --> $new"
  fi
done

exit 0

NOTE: `find... | while read file` is _usually_ equivalent to `for file in find...`

Offline

Board footer

Powered by FluxBB