LinuxQuestions.org
Register a domain and help support LQ
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-27-2014, 07:07 AM   #1
VijayB
LQ Newbie
 
Registered: Jun 2014
Posts: 21

Rep: Reputation: Disabled
how to move seleted directories to specific directories by putting condition


Hi,

I am trying to make a bash script , that will move some directories to specific directories by putting in a condition.

For Example if i have directories with the name say :

1) XYZ
2) XYZ_ABC
3) DEF_GHI
4) DEF

Now i want every directory with the name starting with XYZ_XXXX to be moved in the directory XYZ & directory name starting DEF_XXXX should be moved to the directory DEF.

I am trying with for loop but with no luck.

Any suggestions?
 
Old 06-27-2014, 09:50 AM   #2
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,580
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
The following assumes that all the parent directories you want to end with are 3 characters as you indicated (e.g. XYZ, DEF) and that all of your subdirectories include the parent and the underscore (XYZ_*, DEF_*) as shown in your example:

Code:
for dir in $(ls -d ???)
do echo Main Directory is ${dir}
   for subdir in $(ls -d ${dir}_*)
   do echo Subdirectory being moved is $subdir
      mv $subdir ${dir}/${subdir}
   done
done
It also assumes the parent you want to end with already exists. If not you have to do the mkdir of the parent first. Assuming it does NOT exist and that the name component of the existing directories you're trying to move always separated by an underscore as you show you could script that part as well:

Code:
for dir in $(ls -d * |awk -F_ '{print $1}' | sort -u)
do echo Creating Main Directory ${dir}
   mkdir $dir
   for subdir in $(ls -d ${dir}_*)
   do echo Subdirectory being moved is $subdir
      mv $subdir ${dir}/${subdir}
   done
done
 
Old 07-05-2014, 01:35 AM   #3
VijayB
LQ Newbie
 
Registered: Jun 2014
Posts: 21

Original Poster
Rep: Reputation: Disabled
Thanks Nero .. i modified the code a bit to suit actual requirement & it worked fine.
 
Old 07-05-2014, 04:31 PM   #4
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 6,580
Blog Entries: 14

Rep: Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969Reputation: 969
Glad it helped.

You might want to share your final script in case it would help others with similar requirements. You should also go to thread tools and marked this as solved as it helps others searching for answers to more quickly find those with solutions.
 
  


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
script to move and rename sub directories ! papampi Linux - Newbie 6 06-25-2012 02:33 AM
need to move several directories craftereric Linux - Newbie 3 06-02-2008 05:57 PM
Howto move entire directories ? coolcargo Linux - Newbie 18 03-04-2007 12:30 AM
Move certain files into different directories facets Programming 6 05-28-2004 02:20 PM
Can I move Untarred directories? Nigel_Tufnel Linux - General 5 08-08-2002 09:45 AM


All times are GMT -5. The time now is 11:05 AM.

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