Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 26, 2013, 05:44 PM   #1
supertonic
macrumors member
 
Join Date: May 2010
Renaming folders in Terminal

Have hundreds of folders with a 3-digit numbers at the end like this:

sun101
moon light347
earth is round209


Folder names vary in length but there is always a 3-digit number at the end.


Need to remove the 3 numbers at the end & insert them in the beginning like this:

101sun
347moon light
209earth is round


Is it possible to do this in Terminal?


thanks much
supertonic is offline   1 Reply With Quote
Old Jun 26, 2013, 07:27 PM   #2
subsonix
macrumors 68040
 
Join Date: Feb 2008
It's possible, but where are the folders located? You can make a script that match the form you described then rename it. You can then use that script in a loop over a folder of files and folders. Just keep in mind that commands such as mv are destructive so you want to make sure that you test such a script and make sure you understand what it does.
subsonix is offline   1 Reply With Quote
Old Jun 26, 2013, 08:38 PM   #3
kaydell.leavitt
macrumors regular
 
Join Date: Apr 2010
Matching Patterns in the Unix Command Line

My first thought would be to list the files with:

ls

http://learnbymac.com/doc/unix/bsd/ls.man.html

and then do pattern matching with

grep

http://learnbymac.com/doc/unix/bsd/grep.man.html

In a command such as the following:

ls -R | grep '^.*\d\d\d$'

This is just to start giving you an idea. The following expression:

'^.*\d\d\d$'

is called a "regular expression". Regular expressions are used for pattern-matching and for splitting things into pieces.

In this regular expression the '^' character specifies the beginning of the file or folder name, followed any ".*" which means any number of any characters, and then "\d\d\d" specifies exactly three digits and finally "$" specifies the end of the file name.

This command:

ls -R | grep '^.*\d\d\d$'

isn't exactly finished, but I wanted to give you an idea and something to google for (i.e. "split" and "regular expression").

Other useful commands may be

sed, awk, csplit

I'm looking for people to teach Mac programming to. The service is free, I just want to get ideas for my web tutorials on my website.

-- Kaydell
kaydell@yahoo.com
http://learnmacprogramming.com
kaydell.leavitt is offline   0 Reply With Quote
Old Jun 27, 2013, 10:02 AM   #4
subsonix
macrumors 68040
 
Join Date: Feb 2008
Code:
#!/bin/bash

# match the pattern
ORIGINALNAME=$(echo "$1" | grep -P "^[A-z ]+\d{3}$")

# if it doesn't match, quit
if [ -z "$ORIGINALNAME" ]
then
    exit 1
fi

# if it's not a directory, quit
if [ "$(file -ib "$1")" != "directory" ]
then
    exit 2
fi


# separate the numeric suffix from the name
SUFFIX=$(echo "$1" | grep -oP "\d{3}$")
NAME=$(echo "$1" | grep -oP "^[A-z ]+")

# reverse the order
NEWNAME="$SUFFIX""$NAME"

# test the result
echo "$NEWNAME"
This will match the name of folders and print out the new name, to use it in a folder you can do like this:

Code:
for i in * ; do ./thescript.sh "$i" ; done
subsonix is offline   0 Reply With Quote
Old Jun 28, 2013, 04:46 PM   #5
sero
macrumors member
 
Join Date: Aug 2008
oneliner
Code:
for i in $(ls -p|grep "/"|rev|cut -c2-|rev|sed s/\ /_/g);do thr=$(echo "$i"|rev|cut -c1-3|rev); rest=$(echo "$i"|rev|cut -c4- |rev); mv "$(echo "$i"|sed s/_/\ /g)" "$(echo ${thr}${rest}|sed s/_/\ /g)";done
sero is offline   1 Reply With Quote
Old Jun 28, 2013, 09:32 PM   #6
subsonix
macrumors 68040
 
Join Date: Feb 2008
Just a heads up, that will rename all folders regardless of their name.
subsonix is offline   0 Reply With Quote
Old Jun 29, 2013, 01:16 PM   #7
supertonic
Thread Starter
macrumors member
 
Join Date: May 2010
Thanks for all your suggestions. I couldn't get any of these solutions to work & finally figured out some folder names started with underscore "_" . After removing those, these solutions worked.

thanks again
supertonic is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
forklift users-renaming files/folders mrjayviper Mac Applications and Mac App Store 1 Mar 2, 2014 12:52 PM
help with better finder rename (renaming various folders) mrjayviper Mac Applications and Mac App Store 1 Jan 20, 2014 07:13 AM
Outlook 2011 for Mac Offline Folders to Exchange Folders meymetkaplan Mac Applications and Mac App Store 0 Jan 23, 2013 09:34 AM
ApplescriptEditor; Renaming folders CountrySeacow Mac Programming 9 Dec 11, 2012 05:57 PM
Copy HD or individual/folders files via Terminal? kimfjeld OS X 2 Dec 4, 2012 06:43 AM

Forum Jump

All times are GMT -5. The time now is 03:14 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC