Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Linux File System

I have a directory with 1500 folders. I am looking to use an excel document that has the 1500 folder names in one column and a unique id in the next. I am looking to script it so the script reanames the folders to the new unique id. Can anyone help me seems very easy although I do not know how to write scripts. Can anyone provide a link to a script online ( I have not been able to find one).

Look at volume A for folder Var#1 (from excel)

if present on Volume A then mv Var#1 to Var#2 (from excel)

loop and grab next line of excel doc
share|improve this question
1  
If this isn't answered when I get home I will be back, but until then - start with exporting the excel to a CSV. Then from there you could use a combination of rename and awk. –  nerdwaller Nov 1 '12 at 20:52
    
?: Are all the folders in the same directory, or do you need it to scan cross-system for folder A? –  nerdwaller Nov 1 '12 at 21:02
    
I agree that exporting to a CSV and working from there is a good approach — except that gets tricky if any of the old names have commas in them. An alternative is to export to a Tab delimited text file. Of course, if the old names contain tabs, we have the same problem. And as @Richard pointed out, spaces and tabs in the old names cause issues with most solutions. –  Scott Nov 1 '12 at 23:21

3 Answers 3

Ok, here it is:

for i in $(cat test.csv)

do

dir1=$(echo $i|sed 's/,.*//')

dir2=$(echo $i|sed 's/.*,//')

if [ -d $dir1]

then

if [ -d $dir2 ]

then

echo $dir2 present, skipping move from $dir1 to $dir2

else

mv $dir1 $dir2

fi

fi

done

share|improve this answer
    
Not sure if you tested that or not, but that does not work. It prints "cat\ntest.csv" as ${i} You need to wrap "cat test.csv" in $(cat test.csv) –  nerdwaller Nov 1 '12 at 22:29
    
Then it should work. –  nerdwaller Nov 1 '12 at 22:36
    
@nerdwaller: Give the guy a break! He typed for i in `cat test.csv` without realizing how Super User would interpret the back-ticks as formatting controls. –  Scott Nov 1 '12 at 23:13
    
I attempted to edit my comment but it was too late per SU's policy. So I do owe an apology for that coming across negatively (unintentional). –  nerdwaller Nov 2 '12 at 0:08
1  
@nerdwaller: I was able to find three ways to get back ticks to show up: (1) Use backslashes. (2) Type four spaces before each line of code. (3) Type <pre> at the beginning of your code block and </pre> at the end. See the Super User Markdown Editing Help, particularly the Preformatted Text and Inline HTML sections. –  Scott Nov 2 '12 at 22:03

So, I'm a linux, unix, and mac user and write perl and bash scripts. If you have a power shell or cygwin you can do the follow, Windows might have this too but don't hold your breath:

Without doing the whole thing, I think you should be able to take this and mod the way you want...

  • Save your data to a csv file (somefile.csv)
  • In a bash shell

bash> for temp_line in cat somefile.csv

do

echo mv $temp_line |sed 's/,/ /'

done >somefile.sh

Don't run the following line until you are sure it's going to do what you want it to do!

bash>. ./somefile.sh

That should be it... Note, this is a quick hack, this can be written with more error checking and what nots, and this was off the top of my head:) Another issue you might run into is spaces in the file/dir names, just add quotes. If you need to check if the directory is present you can add an if statement like the following:

bash>if [ -d 'dir_name' ]; then ... ;fi

That should be enough nuggets without writing it. I might get inspired later and write this in a follow up post.

share|improve this answer

This is not an answer, but I’m entering it as one because comments don’t allow decent formatting, or this much text.

Obviously, the names of the existing folders are unique (assuming they are either all in the same super-folder, or they are presented as full pathnames — as pointed out by @nerdwaller).  And the unique IDs in the second column are, by definition, unique.  Are the two columns unique (non-overlapping)?  Consider the following data:

          Old name            New unique ID
To_be,_or_not_to_be,                1
that_is_the_question.               2
The_quick_brown_fox                 3
jumps_over_the_lazy_dog.            4
             ⋮                     ⋮
Once_upon_a_midnight_dreary,       40
while_I_pondered,                  41
weak_and_weary,                    42
             ⋮                     ⋮
The_answer_to_the                  56
ultimate_question_of_life,         57
the_universe,_and_everything_is    58
42                                 59
And_so_on...                       60

If/when you try to rename weak_and_weary, to 42, you will collide with the existing 42 folder.  Probably the best way to address this is to check the old folder names for names that match the pattern of the unique IDs, and handle them manually.

share|improve this answer
    
Thanks to all that have responded with solutions for my issue. I will be working on this over the next week or so. I will let you all know how it goes or if I run into any issues that are show stoppers. –  Mike Kohn Nov 2 '12 at 20:23
    
@Mike: Glad to help, and welcome to Super User. By the way, when you write a comment that’s targeted at specific people, it’s conventional to mention their names, preceded by “@” symbols, as in “@Richard, @nerdwaller”. That way we get notified. See the Replying in comments paragraphs of the Comment formatting section of the Markdown Editing Help page. –  Scott Nov 2 '12 at 22:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.