Friday, 23 December 2011

Bash script to move all files into a directory named for them

Bash script to move all files into a directory named for them

#!/bin/bash

clear

echo "################################################# ###############"
echo "Hello," $USER
echo "Bash version is" $BASH_VERSION
echo "Current directory is" $PWD
echo "Changing directory..."
cd /home/admin/Test
echo "Working directory changed to:" $PWD
echo "################################################# ###############"

# save and change IFS
OLDIFS=$IFS
IFS=$'\n'

for FILE in *
do
DIR="$FILE"
echo "Processing" $FILE
mkdir ${DIR%.*}
echo "Creating Directory For" ${FILE%.*}
mv $FILE ${DIR%.*}
echo "Moving $FILE into Directory"
echo "Processing next file..."
done

Hope this *helps someone*

Enjoy

No comments:

Post a Comment