Script to move directories based on first letter to a new directory of that letter
Basically what i am trying to do is this.
/Blah/A.blah.blah /Blah/A.blah2.blah2 /Blah/B.blah.blah /Blah/B.blah2.blah2 /Blah2/A2.blah.blah /Blah2/A2.blah2.blah2 /Blah2/B2.blah.blah /Blah2/B2.blah2.blah2 I want to move all the A* directories into /A and all the B* directories into B. Now each one of those have files in them which I would like to move as well. I've tried Code:
find . -type d -iname "A*" -exec mv {} /A \; Any help with this script would be great or how to automate this better. |
After messing around with this some more, I still havent been able to automate this but this is what I have.
Code:
find . -type d -iname "A*" | cut -f 3 -d "/"| sed '/^[ ]*$/d'| awk '{print "mv " $1 " /A/"}' > move.sh I would like to somehow automate this so I dont have to go down the list of A-Z and just run one script and it would do it. |
A quick and dirty way would be to have a script containing 26 lines, one for each letter of the alphabet, and on each line, instead of saying:
> move.sh you say: >> move.sh to append any new commands to the end of the already-existing file. If you do this, you'll want an additional three lines at the beginning: rm -f move.sh touch move.sh chmod 700 move.sh The first of those lines will remove any move.sh that exists from a prior run. The second one creates an empty move.sh which you can execute (because you've changed the protection to 700) without error even if it's empty. Two improvements are suggested. The first is to suppress error messages, if you wish. The phrase 2> /dev/null is recommended. Try experimenting with it in different places in the script, so the only time error messages are suppressed is if there are no files beginning with a certain letter. If you want to suppress errors at a particular point in piped commands, use parentheses (surrounded by spaces). This works by firing off a subshell to get the work done, but you don't need to know that. Example of a piped command where only a certain command in the middle has its error messages suppressed, but the other error messages appear, is this: alpha | ( beta 2> /dev/null ) | gamma | delta The other thing I'd suggest (but it's less important) is that if you want to not have 26 separate commands for the 26 letters of the alphabet, try this example: for xxx in A B C D E F G H I J K L M N O P Q R S T U V W X Y Z do echo $xxx done ... and then plug in your command, using $xxx instead of A. Notice that the xxx is not preceded by the dollar sign on the "for" line, but is preceded by the dollar sign elsewhere. Hope this helps. |
Ok so I am taking your advice and im still having some problems :(
At first I was trying with Code:
awk '{print "mv " $1 $i}' > test2 Any help would be great ... Thanks :) Code:
#!/bin/bash |
After further testing, this is what I have now... I noticed that at times my previous code would want to move the main directory prior to moving its subdirectories. I am trying to put in an IF statement to ignore just a mv /ABC /A and only do mv /ABC/ABC2 /A
Code:
#!/bin/bash |
I'm not sure what your current question is (grin), but I'd like to focus on something you said earlier:
Quote:
What you want the shell to do is substitute a particular letter for the $i, but pass the $1 to awk just as it is. Try this: Code:
for xxx in A B C What you shouldn't do is this: Code:
awk "{print "mv " $1 $i}" > test2 Try this: Code:
echo "abc \"def\" ghi" Code:
awk "{print \"mv \" $1 $i}" > test2 Code:
for xxx in A B C So what you want is this: Code:
awk "{print \"mv \" \$1 $i}" > test2 Incidentally, I'd recommend that you stay away from single-letter variables such as i. A problem arises if later you use a text editor to look for each occurrence of that variable. You'll also find all other i's in the script, even if they're part of a comment or the i in "if", for example. That's why I used xxx in my prior post. Hope this gets you closer to where you want to be. |
Thanks, that did help and I did learn from that. I still cant get the $xxx variable to print from the awk command. Ideally I would like it to ignore just the /Blah from the outputs and only use /Blah/A.Blah. I guess I would like to use the Find command to only search through subdirectories and out put full path ??
My current code is this Code:
#!/bin/bash Code:
mv /Blah Code:
/Blah Code:
mv /Blah/A.Blah /A |
Ok after some googling I found the -mindepth variable with Find. I am still now able to output $xxx through awk. With this I dont need the Cut command. I still need the sed to remove empty/blank lines.
Code:
!/bin/bash Code:
mv /Blah/Z.blah |
I finally got something I can work with :) Thanks for all the help
Code:
#!/bin/bash |
All times are GMT -5. The time now is 04:30 AM. |