find/xargs question...

Cameron Simpson cs at zip.com.au
Thu Dec 28 10:56:44 UTC 2006


On 27Dec2006 23:40, bruce <bedouglas at earthlink.net> wrote:
| i'm trying to search through a dir tree for files matching certain patterns
| and i want to rename the files. i'd also like to ignore certain dirs.
[...]
| i'd like to find any file with "zu_fl*Class"

  find the-dir -type f -name 'zu_fl*Class*' -print

| and replace it with
| zu..ClassFFFF. so basically, i'm finding any given file with a pattern
| followed by Class, and adding FFFF to it along with the file extension.

For me, I'd munge the filename with sed - there are other choices.
A sed command like this:

  sed 's|^\(.*/zu_fl[^/]*Class\)\([^/]*\)$|mv & \1FFFF\2|'

That should get you the original name plus the changed name, as a mv
command. Then feed it to the shell.

| i'd also like to ignore specific dirs as well... in this case, i'd like to
| ignore the ".svn" folder...

You need to insert "-type d -name .svn -prune".

So the whole thing might look like this:

  find the-dir -type d -name .svn -prune -o -type f -name 'zu_fl*Class*' -print \
  | sed 's|^\(.*/zu_fl[^/]*Class\)\([^/]*\)$|mv & \1FFFF\2|'

That's totally untested. test it and fix it! When it's emitting the "mv"
commands you want, pipe it to the shell:

  find the-dir -type d -name .svn -prune -o -type f -name 'zu_fl*Class*' -print \
  | sed 's|^\(.*/zu_fl[^/]*Class\)\([^/]*\)$|mv & \1FFFF\2|' \
  | sh -x

Things to note:

  That -o in the find is an "OR". There are implicit ANDs ("-a" if you
  want to be explicit) between find predicates by default. Since AND
  binds tighter than OR, we don't need and tacky parentheses.

  The sed is fairly complicate because I'm constraining the filename
  match to the last path component. Otherwise have a directory matching
  your pattern, containing a file that also matches the pattern, would
  get nasty.

And there's no quoting in the mv commands. So if you have some tricky
filenames (eg containing spaces) you'll have to work harder.

No manky xargs needed.

Cheers,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

Almost nothing in Perl serves a single purpose. - Larry Wall in <199712040054.QAA13811 at wall.org>




More information about the fedora-list mailing list