Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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 have mulitple folder names in a hierarchy that need to be changed. For instance, "Sandbox" is the first directory in my root. It has hundreds of folders in it, and those folders have several folders within them that are consistently named. For example;
C:
/Sandbox/Folder One/Next Folder/Next Next Folder
/Sandbox/Folder Two/Next Folder/Next Next Folder
/Sandbox/Folder Three/Next Folder/Next Next Folder
I would like to be able to change all folders named "Next Next Folder" to, say, "Folder Level 2" recursively. In my newbieness, I have tried the following command (without sucess, I might add):
Is it even possible to change subdirectory names within subdirectories without cd'ing down the whole filepath? I cannot find anything on this question on the "internets."
Thank you, in advance, for your help.
Last edited by SuzDouk; 10-30-2015 at 12:58 PM.
Reason: Re-edited for clarity
Is it even possible to change subdirectory names within subdirectories without cd'ing down the whole filepath? I cannot find anything on this question on the "internets."
Sure, you just need to use dirname and basename to split the directory from its parents, so you can rename it and then put it back in the same directory structure, eg:
Code:
for i in Sandbox/*/*/Next\ Next\ Folder; do
thisparent=$(dirname "$i")
thischild=$(basename "$i")
newchild=${thischild/Next Next Folder/Folder Level 2}
mv "$i" "$thisparent/$newchild"
done
I would be cautious doing it recursively in one command with a find, because the find runs once and then then loop is executed on the result. As soon as you rename one directory the names of all of the children will be different, and subsequent iterations of the loop will fail because the names returned from find will no longer be valid. I would do it one level at a time, which is easy with globbing patterns.
Last edited by suicidaleggroll; 10-30-2015 at 01:25 PM.
Firstly, thank you for taking the time to help me with this command.
Secondandly, when I run this command (substituting "Next Next Folder" with "INVESTIGATOR FINANCIAL CERTIFICATIONS" the true name of the folder, I get the error - mv: cannot stat Sandbox/*/*/INVESTIGATOR FINANCIAL CERTIFICATIONS: No such file or directory.
Substituting your example with my actual filename, these are the commands I ran:
for i in Sandbox/*/*/INVESTIGATOR\ FINANCIAL\ CERTIFICATIONS; do
thisparent=$(dirname "$i")
thischild=$(basename "$i")
Q1: No. My apoligies, the correct folderpath is C:Sandbox/Folder level 2/INVESTIGATOR FINANCIAL CERTIFICATIONS. I did, however, take out one level in the command, e.g. for i in Sandbox/*/INVESTIGATOR FINANCIAL CERTIFICATIONS; do, but I recived the same error.
Q2: Yes. The files and folders are on a Windows machine. I have Cygwin installed to run commands. Is it a DOS vs Unix issue?
You can't run the script I posted in DOS/Powershell, it needs to be in BASH. Cygwin would work, but then you need to modify your path, since C:\ is /cygdrive/c in cygwin. So C:\Sandbox\ becomes /cygdrive/c/Sandbox/.
Its also going to simplify your life in the long run if you can avoid using spaces in file/dir names.
They can be handled by using extra quoting everywhere you reference them but its a PITA and makes code messy/fragile/hard to read.
Consider using underscores.
The underlying problem is that the *nix convention is that spaces are used to separate param values fed to a program/tool.
Just something to be aware of.
HTH
suicidaleggroll: I am using BASH through the Cygwin command-line interface for Windows.
I launch Cygwin, change directory to c:/Sandbox, then followed your example. I am sure it is user error but, being the user, I can't see the error of my ways.
chrism01: I agree with your suggestion. Unfortunately, I work in a customer's environment wherein they have created folders with spaces, and a whole lot of other problematic data.
Thank you for taking the time to educate me on the *nix convention.
If you're already inside the Sandbox directory, then you can't access "Sandbox/*/INVESTIGATOR...", it would simply be "*/INVESTIGATOR...". Unless your path is actually C:\Sandbox\Sandbox\*\INVESTIGATOR...
Forget the loop and the script, and just use ls. "ls -d */INVESTIGATOR..." or "ls -d Sandbox/*/*/INVESTIGATOR..." or whatever it takes to actually list the files/directories you want to rename, and then use that in the script.
Last edited by suicidaleggroll; 11-17-2015 at 11:10 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.