Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Arranging Files

Status
Not open for further replies.

Marie012

Technical User
Dec 7, 2002
12
GB
Hi Guys

I'm new to Unix admnistration, i'm currently looking after a set of directories that need to have the files ordered within them into a hierarchy depending on alphabetical order. I've been working on a shell script that would order all files by their first letter and create alphabetical directories accordingly.

It is only to arrange files in the current working directory and create the directories to store the files where appropriate, it doesnt need to create any empty subdirs. anything that is not an ordinary file needs to be left as it is. the twist in this script is I need to create a working list that can be appended to in text form to list the files that have been reordered so that I can keep an eye on where files are moving to.

So far I have came up with: -

#!/bin/sh

ls|while read xx
do
if [ -f $xx ] ;then
dir=$(echo $xx|awk '{print substr( $1, 1, 1)}')
if [ ! -d $dir ] ;then
mkdir $dir
fi
mv $xx $dir
fi
done

But I cant get it to work and its giving me a huge headache lol, any help would be greatly appreciated!

Marie
 
for file in `ls`
do
case $file in .*) continue # save in case you use '-a'
;; ?) [ -d $file ] && continue
mv $file $file.$$
file=$file.$$
;; esac
dir=`echo $file|cut -c 1`
[ -d $dir ] || mkdir $dir || exit 1
mv $file $dir
done

nota you get problem with 'ls -a' because of the '.' -----------
when they don't ask you anymore, where they are come from, and they don't tell you anymore, where they go ... you'r getting older !
 
Marie,

The $(command) syntax that you are using only works in Korn shell (ksh). In Bourne shell (sh) you need to use the backquote syntax `command`. Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top