Register
It is currently Tue Sep 30, 2014 3:48 am

Create symlinks sorted into A-Z folders


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Tue Dec 02, 2008 7:33 am   

Joined: Tue Dec 02, 2008 7:11 am
Posts: 3
How would I create symbolic links from everything in a source folder to alphabetized subfolders of a destination folder?

Code:
$ tree example
example/
|-- destination
|   |-- a
|   |   |-- Afoo
|   |   `-- afoo
|   `-- b
|       |-- Bfoo
|       `-- bfoo
`-- source
    |-- Afoo
    |-- Bfoo
    |-- afoo
    `-- bfoo


Top
 Profile  
 PostPosted: Tue Dec 02, 2008 8:09 am   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
As far as I know there aren't any good ways of printing out the alphabet, so I'll do this one on the fly.
Code:
#!/bin/bash
to_path=$1

for i in *; do
    first_letter=$(echo $i|cut -b1)                                  # Chop out the first "byte" (the first letter) from $i
    [ -d $to_path/$first_letter ] || mkdir -p $to_path/$first_letter # Check if the directory exists or create it
    ln -s $i $to_path/$first_letter/$i                               # symlink it
done


This thou requires you to be positioned in the source directory. You can elaborate this to handle the source directory as an parameter but this is basically how it can be done :)

ps. Looks like this could even be a nice one-liner :P ds.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Tue Dec 02, 2008 8:16 am   

Joined: Tue Dec 02, 2008 7:11 am
Posts: 3
Wow, I was making this way too complicated. :) Thanks.

Here's what I ended up with:

Code:
#!/bin/bash

destdir="/media/akml01/ml/by-alphabet"
srcdir="/media/akml01/ml/main"

die() { echo "$(color red)$1$(color off)"; exit 1; }

rm -rf $destdir/* \
   || die "Failed to clear $destdir/"

for item_src in $srcdir/*; do
   item="$(basename "$item_src")"
   letter="$(echo "$item" | tr -c "[:alnum:]" 1 \
                          | tr "[:digit:]" 1 \
                          | tr "[:upper:]" "[:lower:]" \
                          | cut -c1)" \
      || die "Failed to index $item_src"
   echo "$letter: $item_src"
   mkdir --parents $destdir/$letter \
      || die "Failed to make directory $destdir/$letter"
   ln --symbolic --target-directory="$destdir/$letter/" "$item_src" \
      || die "Failed to link to $item in $destdir/$letter/"
done


I made the sort case-insensitive and shoved all numbers, punctuation, and nonstandard characters into the "1" subfolder.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP