#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Ssh command to rename files


    What is the ssh commands to rename a group of files?

    I need to rename:
    avatar_01.jpg
    avatar_02.jpg
    etc.

    to
    _01.jpg
    _02.jpg
    etc.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Jun 2007
    Posts
    1,513
    Rep Power
    1424
    A combination of
    find, mv and sed should do the trick.

    Regards, Jens

    Comments on this post

    • jzd agrees
    Last edited by JClasen; December 23rd, 2009 at 06:49 AM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0
    could you show me the entire command. I am new to ssh.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0
    Is there anyone out there to show me exactly what I would do?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,451
    Rep Power
    1751
    ssh is, strictly, just a way of connecting - you will actually be running another shell (probably bash).

    If you need to find a set of files with names starting with avatar_ you can use:

    Code:
    find /directory/where/files/are -type f -name avatar_\*
    The find command has a -exec option (see the man page) which will run the command specified on each returned result. Thus in this instance for every file that starts with avatar_. All you then need to be able to do is remove the avatar part of the name and use that as part of the mv command: mv old_name new_name, which would mean somethign akin to:
    Code:
    find . -type f -name avatar_\* -exec mv {} new_name \;
    Obviously that won't do, so you'd need to arrange it such that a 'chain of commands' gets run passing through both the old name (which is the {} bit of the command - done for you by the find) and the new_name. I tend to fight shy of doing that sort of thing (natural born coward, me!) and end up doing a simple script on the command line, like:

    Code:
    for f in $(ls -1p avatar_\* | grep -v "\$")
    do
      old_name=$f
      new_name=$(echo "$old_name" | sed -n 's/avatar//'p')
      mv "$old_name" "$new_name"
    done
    Not tested, so check first!
    Which assumes that all the files are in current directory.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0
    I manually made the changes since I had to get it done before the last post.

    But thanks anyways, I will save your post and test it sometime

IMN logo majestic logo threadwatch logo seochat tools logo