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!

simple shell script.. plz help!!!

Status
Not open for further replies.

JANET1979

Vendor
Dec 3, 2002
13
GB
i need a simple shell script for unix, however im completely useless at shell scripting and cant get past the #!/bin/sh bit 8(....

what i desperately need to do is move files from my current directory into subdirectories depending on the first letter of its name, (eg.Accounts/A54mJU would be put into a subdirectory called A).

Subdirectories should only be created if there's files to be put into them (ie. no empty subdirectories)

and last of all a file called catalogue.txt should be created in my current directory listing all of the files which have been relocated, grouped by letter....

if any answers or help can be given i would be forever grateful...

thanks... Janet
 
Here's a quick and dirty script to start with. It puts files in the directory without regard to case. You can set it up to make a directory for lower case/upper and even starting number/symbol if you like. e.g. for a directory for each case and all numbers change [A-Z] to [a-zA-Z0-9] and remove the pipe to the tr command. Good luck.

#! /bin/sh
for file in *
do
if [ -f $file ]; then
THE_DIR=`echo $file | cut -c1 | tr -s "[:lower:]" "[:upper:]"`
case $THE_DIR in
[A-Z])
if [ -f "$THE_DIR" ]; then
echo "The directory ($THE_DIR) exists as a file!" 1>&2
continue
elif [ ! -d "$THE_DIR" ]; then
mkdir "$THE_DIR"
fi
;;
*) echo "Unable to deal with file: $file"
;;
esac
mv $file "$THE_DIR"
fi
done Cheers,
ND [smile]

bigoldbulldog@hotmail.com
 
#!/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

I did not test it.
I hope it works.

regards gregor.
Gregor.Weertman@mailcity.com
 
hi,

first of all thanks gregor for your help.

however i was wondering could you explain what the following lines actually do. After hours trying to figure out exactly what it means, ive ended up with a rough idea.

If you could help it would really stop me from going completely mad (lol). thx again.

ls|while read xx
do
if [ -f $xx ] ;then
dir=$(echo $xx|awk '{print substr( $1, 1, 1)}')
if [ ! -d $dir ] ;then
mkdir $dir
 
Of course, it will be my pleasure.

ls|while read xx
# The read command reads from stdin.
# Type: read xx
# Type bla
# echo $xx
# You will see $xx = bla
# Ls pipes it to read. It disables the keyboard.

do
if [ -f $xx ] ;then
# Test if $xx is a regular file.

dir=$(echo $xx|awk '{print substr( $1, 1, 1)}')
# echo $xx|awk '{print substr( $1, 1, 1)}'
# Pipe $xx to awk and print a substring from the first
# field beginning at the first char 1 char long
# $( )-> means execute the command
# the output of:
# echo $xx|awk '{print substr( $1, 1, 1)}'
# will be assigned to dir

if [ ! -d $dir ] ;then
# check if $dir is not (!) a directory
mkdir $dir
# Create the directory $dir

Please say sane Janet.
I hope this is enough to contribute to that.
When not, just ask.

Regards Gregor Gregor.Weertman@mailcity.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top