ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
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|' '{}' ';'
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.