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....?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
A good way to do both these is using the following script:
#!/bin/bash
DUMPDIR=/tftpimage/configs
cd $DUMPDIR
ERRFILE=$DUMPDIR/errfile.o ut
for i in abcd efgh 1234
do
mv ${i}-* $DUMPDIR/${i}/ 2>$ERRFILE
done
#!/bin/bash
DUMPDIR=/tftpimage/configs
cd $DUMPDIR
ERRFILE=$DUMPDIR/errfile.o
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.o ut
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
#!/bin/bash
DUMPDIR=/tftpimage/configs
cd $DUMPDIR
ERRFILE=$DUMPDIR/errfile.o
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
ASKER
Sorry for the delay - I'll give the above suggestions a shot and see which one works best.
Thanks.
Thanks.
ls|sed -e 's#\([^-]*\)-\(.*\)#mv & \1/\2#'|sh
ASKER
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?
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}${DTST
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}
done
/R
ASKER
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.
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-
[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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
did you test my suggestion also, what was wrong with it?
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