[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

BASH Script to move files

Posted on 2006-05-11
14
Medium Priority
?
1,940 Views
Last Modified: 2010-08-05
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....?
0
Comment
Question by:steve212
  • 5
  • 3
  • 2
  • +3
14 Comments
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 300 total points
ID: 16664116
#!/bin/bash

cd /tftpimage/configs

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

Expert Comment

by:leisner
ID: 16664630
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
 
LVL 1

Expert Comment

by:vishwa_karthick
ID: 16665136
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 8

Expert Comment

by:da99rmd
ID: 16665677
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
 

Author Comment

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

Thanks.

0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16677344
ls|sed -e 's#\([^-]*\)-\(.*\)#mv & \1/\2#'|sh
0
 

Author Comment

by:steve212
ID: 16680045
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
 
LVL 8

Expert Comment

by:da99rmd
ID: 16680647

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

/R
0
 
LVL 8

Expert Comment

by:da99rmd
ID: 16680679

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

Expert Comment

by:da99rmd
ID: 16681171

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
 

Author Comment

by:steve212
ID: 16687822
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
 
LVL 8

Assisted Solution

by:da99rmd
da99rmd earned 100 total points
ID: 16687965

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

/R
0
 
LVL 1

Assisted Solution

by:vishwa_karthick
vishwa_karthick earned 100 total points
ID: 16688866
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 16702677
did you test my suggestion also, what was wrong with it?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Screencast - Getting to Know the Pipeline
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question