Find the answer to your Linux question:
Results 1 to 3 of 3
hi, I've a machine running RHEL3,kernel version 2.4. i need to rename multiple files under one directory as follows: [root@venus ] ls demo.c demo.S demo-1243.sw demo.xyz and now i need ...
  1. #1
    Just Joined! amit4g's Avatar
    Join Date
    Feb 2007
    Location
    Bangalore,India
    Posts
    63

    [SOLVED] Renaming multiple files

    hi,

    I've a machine running RHEL3,kernel version 2.4.
    i need to rename multiple files under one directory as follows:
    [root@venus ] ls
    demo.c demo.S demo-1243.sw demo.xyz
    and now i need to replace the occurrence of demo with demo_1 for each of the above file. the tedious way is to go ahead and do mv on each files, but if i've
    huge number of such files and scattered around multiple directories then, a small
    script would be of great help.
    i think rename might help, but also note that the rename program on 2.4 and 2.6 is different.

    ~amit

  2. #2
    Linux Newbie radoulov's Avatar
    Join Date
    Sep 2007
    Posts
    111
    If you have zsh:

    Code:
    autoload -U zmv
    zmv '(**/)(*demo*)' '$1${2//demo/demo_1}'
    Otherwise:

    Code:
    find -type f -name '*demo*'|
      while IFS= read -r; do
        p="${REPLY%/*}"
        f="${REPLY##*/}"
        mv "$REPLY" "$p/${f//demo/demo_1}"
      done

  3. #3
    Just Joined! amit4g's Avatar
    Join Date
    Feb 2007
    Location
    Bangalore,India
    Posts
    63
    thank you so much radoulov,

    found a solution for this in another forum as well:
    for i in $(ls demo*); do mv $i demo_1${i#demo};done

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...