Link to home
Start Free TrialLog in
Avatar of steve212
steve212

asked on

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....?
ASKER CERTIFIED SOLUTION
Avatar of brettmjohnson
brettmjohnson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of leisner
leisner

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

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
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
Avatar of steve212

ASKER

Sorry for the delay - I'll give the above suggestions a shot and see which one works best.

Thanks.

ls|sed -e 's#\([^-]*\)-\(.*\)#mv & \1/\2#'|sh
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?

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

/R

#!/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

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
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.

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
did you test my suggestion also, what was wrong with it?