1

I'm working in my terminal using bash trying to rename the folders:

1 2 3 4 5

I'd like to rename them according to a.txt file:

a 2 
c 3 
d 5 
e 1 
b 4

The order in the .txt file is completely random. I'm hoping to find a way where I can rename the folder so they become:

1 > e 
2 > a 
3 > c 
4 > b 
5 > d

Is there any way to do this? Note that I'm using OSX.

closed as off-topic by terdon May 31 '16 at 11:24

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question has been posted on multiple sites. Cross-posting is strongly discouraged; see the help center and community FAQ for more information." – terdon

  • Hang on, is your file one line or many? Also, are all the target directories in the same place? What else is there in that directory? – terdon May 31 '16 at 10:29
  • My file has two colums with each 194 lines. All target directories are in the same "parent directory" as sub-folders. There is nothing else in the "parent directory" than the sub-folders (that I want to rename) and a txt file with the 194 lines. – Ditte May 31 '16 at 10:38
  • OK. Please edit your question to include this information. Comments are easy to miss, hard to read and can be deleted without warning. Also tell us whether your target directory names can contain spaces. – terdon May 31 '16 at 10:44
3

In the simple case you show above, where each line has two "words", the target directory and the new name, and where neither can contain any whitespace, you can simply do:

while read -r from to; do mv "$from" "$to"; done < file 

That will read each line in file assign the first string (until the first whitespace) to $from and the rest of the line to $to and then run mv on each of them.

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