Rename files recursivly in specified directories


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Rename files recursivly in specified directories
# 1  
Old 07-13-2008
Rename files recursivly in specified directories

Hi,

This is what I would like to do.

1. Find all directories named "ByHost" in a specified directory
2. Rename all .plist files inside "ByHost" directories

This is the way I have been able to do it so far.

Code:
#!/bin/sh
#
# Rename ByHost files
#
# Thomas Berglund, 13.07.08

# Get the Universally Unique Identifier (UUID)
# ioreg commands found at http://www.afp548.com/article.php?story=leopard_byhost_changes
#
# Check if hardware is PPC or early Intel
if [[ `ioreg -rd1 -c IOPlatformExpertDevice | grep -i "UUID" | cut -c27-50` == "00000000-0000-1000-8000-" ]]; then
    LEOUUID=`ioreg -rd1 -c IOPlatformExpertDevice | grep -i "UUID" | cut -c51-62 | awk {'print tolower()'}`

# Check if hardware is new Intel
elif [[ `ioreg -rd1 -c IOPlatformExpertDevice | grep -i "UUID" | cut -c27-50` != "00000000-0000-1000-8000-" ]]; then
    LEOUUID=`ioreg -rd1 -c IOPlatformExpertDevice | grep -i "UUID" | cut -c27-62`
fi

# Rename .plist files with the right UUID
# I have made a replica of the folder structure for testing purposes
for file in ./System/Library/User\ Template/*/Library/Preferences/ByHost/*.plist ; do

    # Make new file name with the right UUID
    # Note to self: \(.*\) = \1 (pattern group 1, backreference) http://www.grymoire.com/Unix/Regular.html#uh-10
    newName="`echo ${file} | sed "s/\(.*\)\.[^\.]*.plist/\1.${LEOUUID}.plist/"`"

    # Rename files
    mv -v "${file}" "${newName}"
done

When using this method, dotfiles are not included, and there is a file called .GlobalPreferences.UUID.plist in every ByHost folder.

When I try using the find command in the for-loop...

Code:
# Rename .plist files with the right UUID
for file in "`find ./System/Library/User\ Template/* -name "*.plist"`" ; do
    # Make new file name with the right UUID
    # Note to self: \(.*\) = \1 (pattern group 1, backreference) http://www.grymoire.com/Unix/Regular.html#uh-10
    newName="`echo ${file} | sed "s/\(.*\)\.[^\.]*.plist/\1.${LEOUUID}.plist/"`"
    #echo "$file"
    # Rename files
    mv -v "${file}" "${newName}"
done

I get this error (I put line breaks in the output to make it readable here in the forum):
Code:
mv: rename ./System/Library/User Template/English.lproj/Library/Preferences/ByHost/.GlobalPreferences.0016cb960b0e.plist\n./System/Library/User 
Template/English.lproj/Library/Preferences/ByHost/com.apple.Bluetooth.0016cb960b0e.plist\n./System/Library/User 
Template/no.lproj/Library/Preferences/ByHost/.GlobalPreferences.0016cb960b0e.plist\n./System/Library/User 
Template/no.lproj/Library/Preferences/ByHost/com.apple.Bluetooth.0016cb960b0e.plist\n./System/Library/User 
Template/no.lproj/Library/Preferences/com.apple.Blue.0016cb960b0e.plist to ./System/Library/User 
Template/English.lproj/Library/Preferences/ByHost/.GlobalPreferences.0016cb960b0e.plist ./System/Library/User 
Template/English.lproj/Library/Preferences/ByHost/com.apple.Bluetooth.0016cb960b0e.plist ./System/Library/User 
Template/no.lproj/Library/Preferences/ByHost/.GlobalPreferences.0016cb960b0e.plist ./System/Library/User 
Template/no.lproj/Library/Preferences/ByHost/com.apple.Bluetooth.0016cb960b0e.plist ./System/Library/User 
Template/no.lproj/Library/Preferences/com.apple.Blue.0016cb960b0e.plist: No such file or directory

If I just echo the $file variable, I get this output, and dotfiles are included (the files outside ByHost directories are included too, which is something I would like to avoid):
Code:
./System/Library/User Template/English.lproj/Library/Preferences/ByHost/.GlobalPreferences.0016cb960b0e.plist
./System/Library/User Template/English.lproj/Library/Preferences/ByHost/com.apple.Bluetooth.0016cb960b0e.plist
./System/Library/User Template/no.lproj/Library/Preferences/ByHost/.GlobalPreferences.0016cb960b0e.plist
./System/Library/User Template/no.lproj/Library/Preferences/ByHost/com.apple.Bluetooth.0016cb960b0e.plist
./System/Library/User Template/no.lproj/Library/Preferences/com.apple.Blue.0016cb960b0e.plist

When I echo the $file variable in the first script, you can see that it skips dotfiles:
Code:
./System/Library/User Template/English.lproj/Library/Preferences/ByHost/com.apple.Bluetooth.0016cb960b0e.plist
./System/Library/User Template/no.lproj/Library/Preferences/ByHost/com.apple.Bluetooth.0016cb960b0e.plist

I have tried adding a second for-loop where I specify the path as ./System/Library/User\ Template/*/Library/Preferences/ByHost/.*.plist. It works, but there must be more efficient way.

Any suggestions?

Last edited by Thomas Berglund; 07-13-2008 at 11:02 AM..
# 2  
Old 07-13-2008
pseudocode
Code:
find /path -name "ByHost" | while read PATH
do
 find "$PATH" -type f  -name "*.plist" | while read FILE
 do
   #do rename
  done
done

# 3  
Old 07-13-2008
Thank you!

Thank you so much! Seems to be working great Smilie

I am pretty new to shell scripting. Ordered a few books from Amazon last week (this and this), but they have not been shipped yet. Looking forward to learn more!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename files from multiple directories along with directory indicator

Hi, Friends, i have a requirement where i need to rename my files residing in multiple sub directories and move them to one different directory along with some kind of directory indicator. For eg: test--is my parent directory and it has many files such as a1.txt a2.txt a3.txt ... (5 Replies)
Discussion started by: gnnsprapa
5 Replies

2. Shell Programming and Scripting

Recursivly rename folders removing characters as required

HI guys here's hoping some on pout the can help I have a large library of epub and mobi file creates some what by calibre. Output of tree listing below I would like to recursively rename the directories removing the brackets and numbers I have been scratching my head over... (4 Replies)
Discussion started by: dunryc
4 Replies

3. Shell Programming and Scripting

Rename the files in all the directories and sub-directories

Hi all, I have more than 12000 files in 46 different directories and each directory has 2 sub-directories named “dat” or “gridded”. Dat sub-directories have files with extension “jpg.dat” and gridded sub-directories have files with extension “.jpg”. I need to... (1 Reply)
Discussion started by: AshwaniSharma09
1 Replies

4. UNIX for Dummies Questions & Answers

Script to rename directories

I am trying to create a script that will search for a directory named bob, and then rename that directory to peter. I do not want the directory path to change. so /folders/bob would become /folders/peter It seems to be renaming the bob folder to peter correctly, but the peter folder ends... (2 Replies)
Discussion started by: gumby456m
2 Replies

5. Shell Programming and Scripting

Find directories with same name and rename them

Hello Im trying to make a script in bash shell programming to find subdirectories with the same name into the same directory and rename one of them!! Could you please help me? Thanks in advance (1 Reply)
Discussion started by: BTKBaaMMM
1 Replies

6. Shell Programming and Scripting

Rename files in sub directories with sequential numbers

I can rename a file with sequential numbers from 1 to N with this script: num=1 for file in *.dat;do mv "$file" "$(printf "%u" $num).txt" let num=num+1 done The script begins with renaming a some.dat file to 1.dat.txt and goes on sequentially renaming other DAT files to... (1 Reply)
Discussion started by: shoaibjameel123
1 Replies

7. Shell Programming and Scripting

Recursively rename directories

I have this directory tree under /apps/myapp/data: imageshack.us/photo/my-images/703/foldersc.png How to recursively rename ONLY directories with 5 digits (00000, 00100, 00200,..., 00007, 00107,...)? I want to add to their name two more zeros: Before: 00107 After: 0000107 Thanks in... (2 Replies)
Discussion started by: Susan_45
2 Replies

8. Shell Programming and Scripting

Rename files and directories with special characters

Hello guys, I was looking for a shell script that removes all the special characters from the files and the subdirectories recursively. I could not locate it any more. Dose any body have a similar script that dose that? Thanks for the help. AV (0 Replies)
Discussion started by: avatar_007
0 Replies

9. Shell Programming and Scripting

Command to recursivly search all html files below the current

hi! i need a command to recursivly search all html files below the current directory for the string "ABCDE". that´s very important to me, thanx!!! (3 Replies)
Discussion started by: inane
3 Replies

10. Shell Programming and Scripting

Rename files/directories based on their name

i have hundreds of directories that have to be renamed. the directory structure is fairly uniform which makes the scripting a little simpler. suppose i have many directories like this */*/*/*abc* (in other words i have similar directory names 3 dirs deep that all contain the pattern abc in... (8 Replies)
Discussion started by: quantumechanix
8 Replies
Login or Register to Ask a Question