LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-28-2008, 04:10 PM   #1
disruptive
Member
 
Registered: Dec 2005
Posts: 76

Rep: Reputation: 15
Scripted folder name changes and recursive


I would like to add a prefix to all directories and all directories within those directories. How can I do this. I also would like to change a few of the files within these folders. Now I can do the last bit with sed, however I need a consistent way of going recursively through the directories and changing the name of each and every directory by adding a prefix. In other words.

Folder1
- Folder2
- Folder3
Folder4
- Folder5

to

PrefixFolder1
- PrefixFolder2
- PrefixFolder3
PrefixFolder4
- PrefixFolder5

Thanks
 
Old 02-28-2008, 04:30 PM   #2
jasohl
Member
 
Registered: Oct 2006
Location: Seattle, Washington
Distribution: Gentoo, LinuxMint, Arch Linux
Posts: 99

Rep: Reputation: 18
you could use the rename command. you could write a script to automate and run the command differently for each directory. i took some programming in the past but i'm not proficiant at scripting so can't give you any specifics. theres a bash scripting HOWTO on tldp.org if your interested. someone else may be able to provide you with some examples or simple scripts.
 
Old 02-28-2008, 04:58 PM   #3
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Here's a python script to do it.
back up data first, it worked on your sample example, but I didn't test it much beyond that.
Code:
import os

for root, dirs, files in os.walk('.', topdown=False):
    for dir in dirs:
        full_path = os.path.join(root, dir)
        new_full_path = os.path.join(root, 'Prefix' + dir)
        os.rename(full_path, new_full_path)
before:
Code:
$ ls *
a.py

folder1:
folder2  folder3

folder4:
folder5
after:
Code:
$ python a.py
$ ls *
a.py

Prefixfolder1:
Prefixfolder2  Prefixfolder3

Prefixfolder4:
Prefixfolder5
EDIT: Didn't know there was an 'execdir', use osor example (next post)

Last edited by angrybanana; 02-28-2008 at 05:02 PM.
 
Old 02-28-2008, 04:58 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
One way to do it is (from the parent directory—in your example, the directory that is the parent of Folder1 and Folder 4):
Code:
find -mindepth 1 -depth -type d -execdir rename 's|/|/Prefix|' '{}' ';'
Alternatively,
Code:
find * -depth -type d -execdir rename 's|/|/Prefix|' '{}' ';'

Last edited by osor; 02-28-2008 at 05:00 PM. Reason: Put in alternative
 
Old 02-28-2008, 05:36 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by angrybanana View Post
EDIT: Didn't know there was an 'execdir', use osor example (next post)
Well, there isn’t always an execdir (but I think it’s on at least GNU and BSD find), so I guess it’s not the most portable thing to use (although it is sometimes convenient).

Btw, you don’t have to use execdir, you can use plain exec as well:
Code:
find * -depth -type d -exec rename 's|(?=[^/]*$)|Prefix|' '{}' ';'
 
Old 02-28-2008, 05:47 PM   #6
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Quick question on rename. My rename doesn't seem to have options for regex. After looking it up, it seems my rename comes from "util-linux-ng". I've heard of a perl rename (I assume this is what you're using), just wondering where I could find it.
 
Old 02-28-2008, 06:51 PM   #7
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by angrybanana View Post
I've heard of a perl rename (I assume this is what you're using), just wondering where I could find it.
I thought it came in the perl tarball (with the other utilities such as a2p, pod2text, etc.), but I guess you have to get it from CPAN.

Btw, you don’t need to invoke perl at all. E.g, in GNU find:
Code:
find * -depth -type d -printf 'mv "%p" "%h/Prefix%f"\n' | while read line; do eval $line; done
Or in BSD find
Code:
find * -depth -type d | while read line; do
	mv "$line" "$(dirname $line)/Prefix$(basename $line)"
done

Last edited by osor; 02-28-2008 at 06:52 PM. Reason: Add quotes for names with spaces
 
Old 02-28-2008, 08:08 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Code:
ls -R /full/path   | awk 'BEGIN{OFS=FS="/";prefix="myprefix";q="\047"}
/^\/.*:$/{
  gsub(/:$/,"")
  org=$0
  $NF=prefix"-"$NF
  cmd = "mv "q  org q" " q $0 q
  print cmd
  #system(cmd)
}'
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cmdline: howto know size of folder (recursive) ? Xeratul Linux - General 9 03-16-2020 09:20 AM
Bash Programming, Recursive/Iterative Calls on Folder Contents gtwilliams Linux - Newbie 1 07-06-2005 06:44 PM
Scripted email doctorwebbox Linux - Software 12 05-19-2005 08:40 AM
Scripted Install donaldsolberg Mandriva 1 10-15-2004 08:50 AM
ifconfig and dhcpcd - scripted papasasha Linux - Networking 4 03-29-2002 04:28 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration