LinuxQuestions.org
Help answer threads with 0 replies.
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 06-24-2012, 04:11 AM   #1
papampi
Member
 
Registered: Jan 2011
Posts: 37

Rep: Reputation: 0
script to move and rename sub directories !


Hi
I need to make some changes to a series of directories and sub directories and I need help !
I have a series of directories : "folder a", "folder b", "folder c", ....
and in these directories there are sub directories "folder 1", "folder 2" ,....
I need to rename "folder 1" in "folder a" to "folder a -folder 1" and move it to main directory. so there wont be any sub directories .
the directory tree is :
Code:
main directory
  -folder a 
     -- folder 1
     -- folder 2
     ....
  -folder b
     -- folder 1
     -- folder 2
....
I want it to be :
Code:
main directoy 
 - "folder a-folder 1"
 - "folder a-folder 2"
 - "folder b-folder 1"
 ....
what is the shell script that can do this for me ?
 
Old 06-24-2012, 04:41 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
first, fix the file names so they don't have spaces in them---Windows somehow tolerates this, but in a real OS like Unix or Linux, spaces in file names = trouble.

Start by defining the individual commands---eg, if you are in "folder_a", you could issue:
Code:
mv folder_1 ../folder_a-folder_1
but, we need it to be generic, so use pwd to get the current folder:
Code:
mv folder_1  ../$(pwd)-folder_1
Once you have the right commands, then put it inside a loop
 
Old 06-24-2012, 04:53 AM   #3
papampi
Member
 
Registered: Jan 2011
Posts: 37

Original Poster
Rep: Reputation: 0
Thanks for your help !
I know how to manually use mv .
the problem is that in the main folder there is over 500 folders where in each folder is some sub-folders with deferent names !
and do that manually takes lots of time .
 
Old 06-24-2012, 04:59 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
As I said, simply put the commands into a loop---for example, in a directory, you could do:
Code:
for filename in *; do
     mv $filename ../$(pwd)-$filename
done
Depending on the directory structure, this might have to go inside another loop---but first get it working for the simple case
 
Old 06-24-2012, 05:05 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
PS;
If you know that directories and sub-directories contain only files (or only directories), then looping thru them is easier. Otherwise, the code will need to test each item to see what it is.
 
Old 06-24-2012, 06:22 AM   #6
papampi
Member
 
Registered: Jan 2011
Posts: 37

Original Poster
Rep: Reputation: 0
thanks will test when I get home !
 
Old 06-25-2012, 02:33 AM   #7
papampi
Member
 
Registered: Jan 2011
Posts: 37

Original Poster
Rep: Reputation: 0
this script do the job for me
thanks to Paul Pedant
Code:
AWK='''
BEGIN { FS = "/"; Mv = "mv \047%s\047 \047%s - %s\047\n" ; }
NF == 3 { printf (Mv, $0, $2, $3); }
'''
find . -type d | awk "${AWK}" | bash
The awk filters out just the ones with two levels, and for each of them makes a mv command to do the rename. This is piped to a shell to actually do the mv commands. The "backslash 0 4 7" is an escape for a single quote, which you will need to enclose your path names in the mv commands because you have blanks in the file names.

For testing, just leave off the bash part and check the commands visually to ensure they do what you want.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Shell Script to rename and move files to a different folder in Windows Simon1984 Linux - Newbie 10 05-18-2012 11:50 AM
Bash script help required - move uncompress and rename truxntrax Linux - Newbie 8 09-21-2009 08:54 PM
Script to Move and rename files scribbl35 Linux - Newbie 1 07-18-2007 10:14 AM
bash: make rename script traverse directories morrolan Programming 2 11-08-2006 11:52 AM
Script to rename directories superior88 Linux - Newbie 4 02-15-2005 11:31 PM


All times are GMT -5. The time now is 08:08 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration