BASH Script to move files

Sort of new to scripting, but here's what I want to do.  

I've got a lot of files that get dumped into a single directory every morning.  These file names only differ by their first four characters:

abcd-samesamesame
dcba-samesamesame
1234-samesamesame (etc.etc)

I have directories for abcd, dcba, 1234, etc.  I'd like a script that takes anything starting with abcd, and automatically dump it into the abcd directory.  Here's what I've got started:

#!/bin/sh

DUMDIR="/tftpimage"
FILE="abcd-*"

cd /tftpimage/configs
mv $FILE $DUMDIR/configs/abcd/

Is there a way to have:

FILE="first four characters of the filename"

 and then

mv $FILE $DUMDIR/configs/"first four chars...."

Hope that makes sense....?
steve212Asked:
Who is Participating?
 
brettmjohnsonConnect With a Mentor Commented:
#!/bin/bash

cd /tftpimage/configs

for i in abcd efgh 1234
do
        mv ${i}-* $DUMDIR/configs/${i}
done
0
 
leisnerCommented:
Its a good idea to make sure the target for a move is a directory and exists
before you start mv'ing files --

A good problem that's often hard to track  unless you're looking for it if if
$DUMPDIR/configs exists and
$DUMPDIR/configs/${i} doesn't -- and if you have one
${i}-* it will move it into
$DUMPDIR/configs/${i}
silently

In the script do
if [ ! -d <target dir> ]; then
  either make it or error
fi

0
 
vishwa_karthickCommented:
A good way to do both these is using the following script:

#!/bin/bash

DUMPDIR=/tftpimage/configs
cd $DUMPDIR
ERRFILE=$DUMPDIR/errfile.out

for i in abcd efgh 1234
do
        mv ${i}-* $DUMPDIR/${i}/   2>$ERRFILE
done
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
da99rmdCommented:
Use this version if you dont know the start of the files:

#!/bin/bash

DUMPDIR=/tftpimage/configs
cd $DUMPDIR
ERRFILE=$DUMPDIR/errfile.out
IFS="
"
for file in ./*
do
   if [ -f $file ]; then
      # Get the 1234 part
      dirpart=`printf "$file" | sed 's/\(.*\)-.*/\1/'`
      # Get the samesamesame part
      filepart=`printf "$file" | sed 's/.*-\(.*\)/\1/'`
      if [ ! -d $dirpart ]; then
         mkdir "$dirpart"
      else
        mv "$file" "$dirpart"
        mv "$file" "$dirpart/$filepart""
        #
        # Use this if you want the only the "samesamesame" as the file names in the dirs.
        #mv "$file" "$dirpart/$filepart""
      fi
   fi
done

/R
0
 
steve212Author Commented:
Sorry for the delay - I'll give the above suggestions a shot and see which one works best.

Thanks.

0
 
ahoffmannCommented:
ls|sed -e 's#\([^-]*\)-\(.*\)#mv & \1/\2#'|sh
0
 
steve212Author Commented:
brett,

That's what I needed, the first four characters are very static, they never change, so a simple solution is what I was after.

I've also added:

DTSTMP=`date +%m%d%y`

however, I can't get it to correctly append to the filename.  It moves abcd-samesame to /$DUMDIR/configs/abcd, but it names it abcd.mmddyy, instead of abcd.samesame.mmddyy.  I'm not sure where I'm loosing the "samesame" part of the filename.

Any last suggestion?
0
 
da99rmdCommented:

He just have the "abcd" part of the name not the hole name in $i.

/R
0
 
da99rmdCommented:

#!/bin/bash

cd /tftpimage/configs

DTSTMP=`date +%m%d%y`

for i in abcd-* efgh-* 1234-*
do
        mv ${i} $DUMDIR/configs/${i}${DTSTMP}
done

/R
0
 
da99rmdCommented:

Sry had a error =(

#!/bin/bash

cd /tftpimage/configs

DTSTMP=`date +%m%d%y`

for i in abcd-* efgh-* 1234-*
do
        # Get the 1234 part
        dirpart=`printf "$file" | sed 's/\(.*\)-.*/\1/'`
        mv ${i} $DUMDIR/configs/${dirpart}/${i}${DTSTMP}
done

/R
0
 
steve212Author Commented:
It's got to be close.  To test things out I added the following to the very end of the script:

echo ${dirpart}
echo ${i}

Here is the error I get when I run the script, followed by the two echos:

[ERROR RETURNED] mv: `test-confg' and `/tftpimage/configs//test-confg' are the same file
[ECHO ${dirpart}]    nothing returned
[ECHO ${i}]             test-confg

So it looks like to me there's something not quite right when identifying the 1234 part.  So, the script is trying to move a file from, and to the same location with the same filename.

0
 
da99rmdConnect With a Mentor Commented:

Hehe copy paste error this line:
dirpart=`printf "$file" | sed 's/\(.*\)-.*/\1/'`
should be
dirpart=`printf "$i" | sed 's/\(.*\)-.*/\1/'`

/R
0
 
vishwa_karthickConnect With a Mentor Commented:
Since you are sure about the length of the initial string, you can also try taking advantage of that using Bash's substring extraction:


#!/bin/bash

DUMPDIR=/tftpimage/configs
cd $DUMPDIR
ERRFILE=$DUMPDIR/errfile.out
DTSTMP=`date +%m%d%y`

for FILANAME in abcd-* efgh-* 1234-*
do
        DIRNAME=${FILENAME:0:4}
        mv ${FILENAME} $DUMPDIR/${DIRNAME}/${FILENAME}.${DTSTMP}   2>$ERRFILE
done
0
 
ahoffmannCommented:
did you test my suggestion also, what was wrong with it?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.