Can anyone help out with this simple command?

Discussion in 'Windows & Other OS Discussion & Support' started by Calinerti, Nov 11, 2008.

  1. Calinerti

    Calinerti New Member

    Joined:
    Sep 6, 2002
    Messages:
    330
    Likes Received:
    2
    Trophy Points:
    0
    Hi,

    Im trying to rename multiple files, using CentOS (fedora). I'm running the following command:

    find -iname .new-account | mv .new-account .new-user

    to change all files named .new-account to .new-user

    I keep getting "cannot stat .new-account: no such file or directory".

    But using just "find -iname .new-account" lists all the files I want changed. Im stuck! I also tried find -iname .new-account | rename 's/\.new-account/\.new-user/' but that doesnt work either (but it works in ubuntu), no error message given.

    Id appreciate any and all input you have! (and yeah, I AM a n00b at linux!)
     
  2. merry

    merry gargouille

    Joined:
    Jun 21, 2002
    Messages:
    962
    Likes Received:
    1
    Trophy Points:
    28
    I'm not sure you can pipe anything to mv; not like this, anyway. This command will find (case-insensitive) all files named .new-account in the current directory and below, then proceed to rename the one in the current dir to .new-user (no output from find reaches mv). 'cannot stat [...]' means there is no .new-account file in the current directory.

    I don't use ubuntu; the rename command appears to be ubuntu-specific.

    I tried the line below on a Mac/bash, but should work mostly anywhere.

    This will find all files named .new-account starting from the current dir downwards; get the path using sed to remove the filename from the path (thus creating a list of paths that are stored successively in $i); cd there and rename the file; cd back and continue to the next file:

    Code:
    for i in `find . -name .new-account | sed -e "s/\/\.new-account$//"`; do cd $i; mv .new-account .new-user; cd -; done
    Note that the dot (and preceding slash) must be escaped in the sed regexp, i.e.
    Code:
    \/\.new-account$
    The dollar sign is necessary to force sed to match the filename in a path like
    Code:
    ./path/to/.new-accounts/.new-account
    I'm not sure why you want to use -iname? If your files do not have identical names, (some are called .new-account, but some are .New-Account') the above will not work, you need to repeat the line above with each filename version in turn.

    [edit]

    Here's a better way:

    http://snipplr.com/view/2736/rename-multiple-files-in-bash-using-regular-expressions/

    For your needs, the one-liner in the post above should be modified to:
    Code:
    for i in `find . -name .new-account`; do j=`echo $i | sed 's/account$/user/'`; mv "$i" "$j"; done
    This is more elegant, as it doesn't cd, but renames using the full path, i.e.
    Code:
    mv /path/to/old /path/to/new
    The syntax for sed might differ by unix/shell, so check it first.

    Also, 'rename' appears to be a perl script available on Debian:

    http://tips.webdesign10.com/files/rename.pl.txt
     
    Last edited: Nov 17, 2008
  3. Malus

    Malus BSD SMASH!

    Joined:
    May 13, 2002
    Messages:
    1,170
    Likes Received:
    3
    Trophy Points:
    0
    Seems like you are making it too complicated to me. Here is a snippet that works for me on FreeBSD:

    Code:
    find . -iname .new-account | sed 's/\.new-account$//' | xargs -n1 -I % mv %.new-account %.new-user
    It could probably be made even simpler, but I think it is a good start.
     
    Last edited: Nov 27, 2008
  4. ET3D

    ET3D Hopeless Dreamer

    Joined:
    Aug 20, 2003
    Messages:
    3,086
    Likes Received:
    90
    Trophy Points:
    58
    It was all much simpler on the Amiga...
     

Share This Page

visited