LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 10-30-2015, 12:55 PM   #1
SuzDouk
LQ Newbie
 
Registered: Oct 2015
Location: Connecticut
Posts: 4

Rep: Reputation: Disabled
Rename subdirectories recurseively


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):

find . -name 'INVESTIGATOR FINANCIAL CERTIFICATIONS' -type f -exec bash -c 'mv "$1" "${1/\/'INVESTIGATOR FINANCIAL CERTIFICATIONS_/InvFinancCert/}"' -- {} \;

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
 
Old 10-30-2015, 01:22 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by SuzDouk View Post
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.
 
Old 11-02-2015, 10:07 AM   #3
SuzDouk
LQ Newbie
 
Registered: Oct 2015
Location: Connecticut
Posts: 4

Original Poster
Rep: Reputation: Disabled
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")

newchild=${thischild/INVESTIGATOR FINANCIAL CERTIFICATIONS/InvFincCert}
mv "$i" "$thisparent/$newchild"
done

Soooo, what am I doing wrong?
 
Old 11-02-2015, 10:19 AM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Do you have directories called "INVESTIGATOR FINANCIAL CERTIFICATIONS" on the third layer down from Sandbox?

Eg: "Sandbox/something/somethingelse/INVESTIGATOR FINANCIAL CERTIFICATIONS"
 
Old 11-02-2015, 11:37 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
I noticed the 'C:' at the start of the post, are these folders/files on a Windows machine?
 
Old 11-03-2015, 03:05 PM   #6
SuzDouk
LQ Newbie
 
Registered: Oct 2015
Location: Connecticut
Posts: 4

Original Poster
Rep: Reputation: Disabled
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?
 
Old 11-03-2015, 04:03 PM   #7
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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/.
 
Old 11-03-2015, 05:39 PM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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
 
Old 11-17-2015, 10:59 AM   #9
SuzDouk
LQ Newbie
 
Registered: Oct 2015
Location: Connecticut
Posts: 4

Original Poster
Rep: Reputation: Disabled
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.

Suzanne
 
Old 11-17-2015, 11:07 AM   #10
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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.
 
  


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
Script To Recursively Enter Subdirectories And Rename Files Sequentially From Scratch padi Linux - Newbie 3 04-25-2015 09:05 AM
Bash script to rename photos in directory & all subdirectories shy_guest Linux - Software 7 09-02-2009 01:40 PM
Bash script to rename photos in directory & all subdirectories shy_guest Linux - Software 2 09-02-2009 05:53 AM
Bash script to rename photos in directory & all subdirectories shy_guest Linux - Software 1 09-02-2009 05:28 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:48 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