Find the answer to your Linux question:
Results 1 to 5 of 5
I need to do a mass rename on files. Example of current file structure: Code: folder1 [uid1] somename.ext somename2.ext folder2 [uid2] somename.ext somename2.ext folder3 [uid3] somename.ext somename2.ext Now, I need ...
  1. #1
    Just Joined!
    Join Date
    Jan 2010
    Posts
    2

    Copy a part of folder name and add into file name. Command forming help

    I need to do a mass rename on files.

    Example of current file structure:
    Code:
    folder1 [uid1]
      somename.ext
      somename2.ext
    
    folder2 [uid2]
      somename.ext
      somename2.ext
      
    folder3 [uid3]
      somename.ext
      somename2.ext
    Now, I need to rename the files, such that the file names contain the parent folders uid, for example:

    Code:
    somename [uid].ext
    So to do this, I think the command should first copy the parent directory's [] part and paste it in the end of the file names.

    I think find command with -execdir might be able to perform this, But I am not sure or is there a better way to achieve the results? I am very much beginner to linux world and commands. Thanks for any help, much appreciated.

  2. #2
    Linux Engineer hazel's Avatar
    Join Date
    May 2004
    Location
    Harrow, UK
    Posts
    955
    Try using the dirname command. It takes a pathname as argument and returns the directory part of the name. If you pass this through grep, you might be able to isolate the part of the name you want.
    "I'm just a little old lady; don't try to dazzle me with jargon!"

  3. #3
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    Perhaps this could help :

    Code:
    #!/bin/bash
    DIR="/var/tmp"
    for d in ${DIR}/folder*
    do
      SUFFIX=${d##* }
      cd "${d}"
      for f in ./*.ext
      do
        NOEXT=${f%.*}
        mv "${f}" "${NOEXT} ${SUFFIX}.ext"
      done
    done

  4. #4
    Just Joined!
    Join Date
    Jan 2010
    Posts
    2
    Thanks for the clear understandable script. I successfully renamed the files after some trial tests on dummy file structure.

    Just one more thing, interested to know, how does the
    SUFFIX=${d##* }
    gets the square brackets part of parent folder?

  5. #5
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    Bash Reference Manual

    Look for the sections ${parameter#word} ${parameter##word}

    word is expanded as with file names (* ? [ ]).

    ${parameter#*<char>} will keep everything after the first occurence of <char> and discard the rest.
    ${parameter##*<char>} will keep everything after the last occurence of <char> and discard the rest.
    ${parameter%<char>*} will keep everything before the last occurence of <char> and discard the rest.
    ${parameter%%<char>*} will keep everything before the first occurence of <char> and discard the rest.

    Bash has got many more useful expansions, too many to be handy right away.
    One that may be very useful is the substitution ${parameter/pattern/string}, if we don't want to always use sed for single line substitution.
    Last edited by nmset; 01-30-2010 at 07:34 AM.
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

Posting Permissions

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