Shell Script for Beginner

I have a folder with lots of file. e.g. a.txt, b.txt, c.txt.... I want to put these files from the source directory and place them in a destination directory in a specific order, such as /destination/a/a.txt, /destination/b/b.txt, /destination/c/c.txt, ......
Please help. Thx :confused:

This is based on the understanding that you want to move files to some directory that is named as /some_destination/<first_letter_of_filename>/

#!/bin/sh

DESTINATION=/tmp
for file in *; do
        first=`echo $file | cut -c 1`
        if [ ! -d "$DESTINATION/$first" ]; then
                mkdir -p $DESTINATION/$first
        fi
        mv $file $DESTINATION/$first/
done

maybe what you want is something more like:

#!/bin/sh

# Get the files
FILES=`ls -1`

for FILE in $FILES
do
    # Get the position of the .
    INDEX=`expr index "$FILE" .`

    # need to subtract 1 (you don't want the .) 
    INDEX=`expr $INDEX - 1`

    DIRNAME=`expr substr $FILE 1 $INDEX`

    if [ ! -d "destination/$DIRNAME" ]; then
        mkdir -p "destination/$DIRNAME"
    fi

    mv $FILE "destination/$DIRNAME"
done

this should handle files for more than 1 character
it could probably use more error checking, etc, but you get the idea.
Or maybe you could use the "for file in *;" format, I'm not familiar with that usage.

An improvement for

INDEX=`expr index "$FILE" .`

first=`echo $file | cut -c 1`

The reasoning is you want to extract the name of the file without the extension.

Try this in the script.

INDEX=${FILE%.txt}

first=${file%.txt}

respectively.

vino