Recursivly rename folders removing characters as required


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Recursivly rename folders removing characters as required
# 1  
Old 04-17-2016
Recursivly rename folders removing characters as required

HI guys here's hoping some on pout the can help

I have a large library of epub and mobi file creates some what by calibre.

Output of tree listing below

Quote:
│** ├── Paddy Whacked_ The Untold Story of the Irish American Gangster (581)
│** │** ├── cover.jpg
│** │** ├── metadata.opf
│** │** └── Paddy Whacked_ The Untold Story of the Iri - T. J. English.mobi
│** └── The Savage City_ Race, Murder, and a Generation on the Edge (619)
│** ├── cover.jpg
│** ├── metadata.opf
│** └── The Savage City_ Race, Murder, and a Gener - T. J. English.mobi
├── Tom Philbin
│** └── Killer Book of Cold Cases_ Incredible Stories, Facts, and Trivia From the Most Baffling True Cr (570)
│** ├── cover.jpg
│** ├── Killer Book of Cold Cases_ Incredible Stor - Tom Philbin.epub
│** ├── Killer Book of Cold Cases_ Incredible Stor - Tom Philbin.mobi
│** └── metadata.opf
├── Tony Bennett
│** └── The Good Life (789)
│** ├── cover.jpg
│** ├── metadata.opf
│** ├── The Good Life - Tony Bennett.epub
│** └── The Good Life - Tony Bennett.mobi
├── Trevor Marriott
│** └── The Evil Within - a Top Murder Squad Detective Reveals the Chilling True Stories of the World's (572)
│** ├── cover.jpg
│** ├── metadata.opf
│** ├── The Evil Within - a Top Murder Squad Detec - Trevor Marriott.epub
│** └── The Evil Within - a Top Murder Squad Detec - Trevor Marriott.mobi
├── Troy Denning
│** ├── Book 1 - A Forest Apart (737)
│** │** ├── Book 1 - A Forest Apart - Troy Denning.epub
│** │** ├── Book 1 - A Forest Apart - Troy Denning.mobi
│** │** ├── cover.jpg
I would like to recursively rename the directories removing the brackets and numbers

Quote:
│** ├── Paddy Whacked_ The Untold Story of the Irish American Gangster
│** │** ├── cover.jpg
│** │** ├── metadata.opf
│** │** └── Paddy Whacked_ The Untold Story of the Iri - T. J. English.mobi
│** └── The Savage City_ Race, Murder, and a Generation on the Edge
│** ├── cover.jpg
│** ├── metadata.opf
│** └── The Savage City_ Race, Murder, and a Gener - T. J. English.mobi
├── Tom Philbin
│** └── Killer Book of Cold Cases_ Incredible Stories, Facts, and Trivia From the Most Baffling True Cr
│** ├── cover.jpg
│** ├── Killer Book of Cold Cases_ Incredible Stor - Tom Philbin.epub
│** ├── Killer Book of Cold Cases_ Incredible Stor - Tom Philbin.mobi
│** └── metadata.opf
├── Tony Bennett
│** └── The Good Life
│** ├── cover.jpg
│** ├── metadata.opf
│** ├── The Good Life - Tony Bennett.epub
│** └── The Good Life - Tony Bennett.mobi
├── Trevor Marriott
│** └── The Evil Within - a Top Murder Squad Detective Reveals the Chilling True Stories of the World's
│** ├── cover.jpg
│** ├── metadata.opf
│** ├── The Evil Within - a Top Murder Squad Detec - Trevor Marriott.epub
│** └── The Evil Within - a Top Murder Squad Detec - Trevor Marriott.mobi
├── Troy Denning
│** ├── Book 1 - A Forest Apart
│** │** ├── Book 1 - A Forest Apart - Troy Denning.epub
│** │** ├── Book 1 - A Forest Apart - Troy Denning.mobi
│** │** ├── cover.jpg
I have been scratching my head over this one all day so any help would be gratefully appreciated
# 2  
Old 04-17-2016
Any attempts beyond scatching your head?
# 3  
Old 04-17-2016
yeah although i got a few splinters from the scratching

I can accomplish this fine with a line of text ie

Quote:
echo "The Evil Within - a Top Murder Squad Detective Reveals the Chilling True Stories of the World's (572)" | sed 's/(.*//'
give me the output i would like to rename to but how to apply this recursively to all directories is currently evading me
# 4  
Old 04-17-2016
Try
Code:
find . -type d -name "*([0-9]*)" | while read FN; do echo mv $FN ${FN%([0-9]*)}; done

This doesn't check for three digits but allows for anything after the first digit; this may need some refinement. If the results comes close to what you need, remove the echo for real action.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 04-17-2016
Quote:
Originally Posted by RudiC
Try
Code:
find . -type d -name "*([0-9]*)" | while read FN; do echo mv $FN ${FN%([0-9]*)}; done

This doesn't check for three digits but allows for anything after the first digit; this may need some refinement. If the results comes close to what you need, remove the echo for real action.
Thanks for the heads up had to add some inverted commas due to spaces in the names so

Quote:
find . -type d -name "*([0-9]*)" | while read FN; do mv "$FN" "${FN%([0-9]*)}"; done
worked a treat Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help required with a file rename shell script

Hello everyone, Posting here after a long time, been away from unix world lately and it seems I have forgotten my shell scripting completely. I have a requirement where a csv file contains following columns: Full Registration VIN Stock... (14 Replies)
Discussion started by: tayyabq8
14 Replies

2. Shell Programming and Scripting

Rename directory removing last characters

Hello - I was looking to write a simple script trying to rename sub-directories chopping off the last n characters. For example: In /home/myname/dir there are three sub-directories: directory1_1, directory2_2, and directory3_3. Is there a simple script to chop off the last... (4 Replies)
Discussion started by: twckfa16
4 Replies

3. Shell Programming and Scripting

Moving files to folders and rename

Hello! I am new to this. I have many files from b_ap00 to b_ap80, and I need to move them to folder 00 to 80 respectively, where b_ap00 is in folder 00, b_ap01 is in folder 01. On top of this, I need to rename the file once they are inside the folder to b_ot, and subsequently run it (ifort -o... (8 Replies)
Discussion started by: krustytherusty
8 Replies

4. Shell Programming and Scripting

Removing folders with specific name/part

Hi, I need a way to remove all folders that contain "Quick" in their names in a directory called /var/tmp... However all attemps I have tried won't work. :wall: I so far tried find /var/tmp -type d -name "Quick" | sudo xargs rm -rf find . -name "Quicklook" -exec rm -rf {} \; find .... (2 Replies)
Discussion started by: pasc
2 Replies

5. UNIX for Dummies Questions & Answers

Removing Extra Folders From a TAR

I use an extremely simple TAR function for files at work and I have a question about cleaning them up. My command is TAR -cvf ExampleTarName.tar then the folder I wish to TAR. When my TAR finishes and I double click it to check it unarchived beautifully (I don't do this with every file, duh)... (5 Replies)
Discussion started by: Dogtown24
5 Replies

6. UNIX for Dummies Questions & Answers

Removing empty folders

Hello, I have a folder that contains all my music. Recently, I started using a different media player, and I let it manage my music folder. It has sorted all my music neatly in folders by artist and album. However, all the old folders that the songs used to be in are still there, yet they are... (2 Replies)
Discussion started by: emveedee
2 Replies

7. UNIX for Dummies Questions & Answers

Rename files to indexnumber in multiple folders

I have multiple subfolders with multiple jpg I want the group of files in each subfolder renamed to their index number (3 digits) in the folder, so: folder/a.jpg = folder/001.jpg folder/b.jpg = folder/002.jpg folder/c.jpg = folder/003.jpg folder2/1.jpg = folder2/001.jpg folder2/2.jpg =... (2 Replies)
Discussion started by: cmanniche
2 Replies

8. Shell Programming and Scripting

Rename files recursivly in specified directories

Hi, This is what I would like to do. 1. Find all directories named "ByHost" in a specified directory 2. Rename all .plist files inside "ByHost" directories This is the way I have been able to do it so far. #!/bin/sh # # Rename ByHost files # # Thomas Berglund, 13.07.08 # Get the... (2 Replies)
Discussion started by: Thomas Berglund
2 Replies

9. UNIX for Dummies Questions & Answers

Find and rename all folders with name X.

Is there a command I can use to rename all directories with a certain name to a new name. For instance from my root directory I want to change all folders named '123' to '321' that are in the root directory or any subdirectory. Thanks in advance! (6 Replies)
Discussion started by: mkingrey
6 Replies

10. UNIX for Dummies Questions & Answers

Removing old folders

All Please help me to remove old files. For example /usr/exp/ - inside this Apr 02 - dir02 Apr 03 - dir03 Apr 04 - dir04 Apr 05 - dir05 Apr 06 - dir06 Apr 07 - dir07 Apr 03 - file03 I want to delete all the folders 2 days before created.Not the fil03 even though it is 2 days old. ... (1 Reply)
Discussion started by: DeepakXavier
1 Replies
Login or Register to Ask a Question