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 ...
- 01-25-2010 #1Just 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:
Now, I need to rename the files, such that the file names contain the parent folders uid, for example:Code:folder1 [uid1] somename.ext somename2.ext folder2 [uid2] somename.ext somename2.ext folder3 [uid3] somename.ext somename2.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.Code:somename [uid].ext
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.
- 01-25-2010 #2
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!"
- 01-27-2010 #3Linux 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
- 01-30-2010 #4Just 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 thegets the square brackets part of parent folder?SUFFIX=${d##* }
- 01-30-2010 #5Linux 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.


