- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: script challenge - file moving
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2003 05:01 AM
06-27-2003 05:01 AM
# ll /prod/temp/ascribe_archives
total 17124
-rw-rw-rw- 1 jmickens users 351465 Jun 26 14:31 20030115_j341281_Morgan_Stanley_Retail_RDD_Q1_2003.zip
-rw-rw-rw- 1 jmickens users 137011 Jun 26 14:31 20030120_j341271_Morgan_Stanley_Heavy_Up_RDD_Q1_2003.zip
The first file needs to be moved to /prod/job1/j341281, and the second needs to be moved to /prod/job1/j341271.
The job number will always appear between the first two underscores, but may not always be the same length (some might have 5 digits, others 6). I also need to leave the file where it is if the production directory that it goes with doesn't already exist. The reason I need to automate this is that these are files that are downloaded from a supplier ftp site every week.
I'm not a high-level script programmer. Any suggestions would be greatly appreciated.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2003 05:06 AM
06-27-2003 05:06 AM
Re: script challenge - file moving
cd /prod/temp/ascribe_archives
ls | grep j341281 | while read LINE
do
mv $LINE /prod/job1/j341281/
done
cd /prod/temp/ascribe_archives
ls | grep j341271 | while read LINE
do
mv $LINE /prod/job1/j341271/
done
Robert-Jan.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2003 05:08 AM
06-27-2003 05:08 AM
Solutiondo
echo ARCH_DIR=`echo $i |sed 's/_/ /g' |awk '{print $2}'`
if [[ ! -d /prod/job1/$ARCH_DIR ]]
then
# Directory does not exist
mkdir /prod/job1/$ARCH_DIR
if (( $? < 1 ))
then
print "error"
fi
fi
cp $i /prod/job1/$ARCH_DIR
if [[ -f /prod/job1/$ARCH_DIR/$i ]]
then
rm $i
else
echo "could not move file $i"
fi
done
This script ensures that the file is not deleted until it is a valid entry in the sub-directory.
Share and Enjoy! Ian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2003 05:10 AM
06-27-2003 05:10 AM
Re: script challenge - file moving
if (( $? < 1 ))
should read
if (( $? > 0 ))
Share and Enjoy! Ian
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2003 05:10 AM
06-27-2003 05:10 AM
Re: script challenge - file moving
#!/usr/bin/sh
cd /prod/temp/ascribe_archives
for FILE in $(ls 2>&-)
do
JOB=$(echo $FILE | sed 's;^[^_]*_\([^_]*\)_.*$;\1;')
[ -d "/prod/job1/$JOB ] || continue
mv $FILE /prod/job1/$JOB
done
Regards.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2003 05:12 AM
06-27-2003 05:12 AM
Re: script challenge - file moving
LIST=$( find /prod/temp/ascribe_archives -type f -print)
for i in $LIST
do
# this is the dest directory
DEST_DIR=$( echo $i| awk -F_ '{print $2}' )
FULL_DIR=/prod/job1/$DEST_DIR
if [-d $FULL_DIR]
then
mv $i $FULL_DIR
else
echo $FULL_DIR not found, skipping file $i
fi
done
HTH,
Massimo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2003 05:17 AM
06-27-2003 05:17 AM
Re: script challenge - file moving
This will get you started:
#!/usr/bin/sh
cd /tmp
ls -l 2003*|awk '{print $NF}'|while read F
do
FF=`echo ${F}|awk -F"_" '{print $2}'`
echo cp ${F} /prod/job/${FF}
done
exit 0
I have assumed that your source files reside in the '/tmp' directory in the above example. I have also assumed that they are named starting with the string "2003". Drop the 'echo' in front of the 'cp' and files will actually be copied.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2003 05:22 AM
06-27-2003 05:22 AM
Re: script challenge - file moving
a little futher check to add, at everyone's reply (mine inclusive):
let's suppone that $i is source
...
cp $i $DEST_DIR/$i
diff $i $DEST_DIR 1>/dev/null 2>&1 && rm $i
...
so you are sure that the file copied was not being edited :)
cheers,
Massimo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2003 05:23 AM
06-27-2003 05:23 AM
Re: script challenge - file moving
a little futher check to add, at everyone's reply (mine inclusive):
let's suppone that $i is source
...
cp $i $DEST_DIR/$i
diff $i $DEST_DIR/$i 1>/dev/null 2>&1 && rm $i
...
so you are sure that the file copied was not being edited :)
cheers,
Massimo
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2003 06:22 AM
06-27-2003 06:22 AM
Re: script challenge - file moving
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2003 06:36 AM
06-27-2003 06:36 AM
Re: script challenge - file moving
This may be lengthy, but I like to see what's going on and keep it simple for the next person.
#!/bin/sh
ARCH_DIR=/prod/temp/ascribe_archives
MV_BASE=/prod/job1
LOG=/tmp/file_mover.log
if [ -f $LOG ] ; then
rm -f $LOG
fi
touch $LOG
cd $ARCH
for FILE in `ls` ; do
NEWDIR=`echo $FILE|awk -F_ '{print $2}'`
if [ -d ${MV_BASE}/$NEWDIR ] ; then
echo "Moving ${ARCH_DIR}/${FILE} to ${MV_BASE}/${NEWDIR}" >>$LOG
mv $FILE ${MV_BASE}/$NEWDIR
else
echo "No such Directory ${MV_BASE}/${NEWDIR}" >>$LOG
fi
done
Regards,
Shannon
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2003 12:23 PM
06-27-2003 12:23 PM