Solved

Rename a changing file name in UNIX?

Posted on 2002-06-26
8
445 Views
Last Modified: 2012-05-04
I would really appreciate if someone could help me solve my problem. I am not a unix programmer but can move around. We can use Korn or Bourne Shell Scripts.

Scenerio:
* File comes into a directory named \Incoming
* The file starts with P84C.125
* The file comes daily with the same filename but the extension increases. Therefore the new file is P84C.126

Requirement:
* I want to take the file P84C.125 and copy it to \Archive folder
* I want to rename the file to "process.dat" and move it to \process folder

note: the file name changes daily. My main problem is to rename the dynamic name of the file to "process.dat"

Thanks all for your help!
0
Comment
Question by:cyberonics
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 6

Expert Comment

by:bira
ID: 7112139
Hi
  Try this

    for i in `ls /Incoming/P84C.*`
    do
    cp $i /process/process.dat
    mv $i /Archive
    done
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7112837
add following to your crontab:

59 23 * * * (cd /Incomming; cp `ls -tr P84C.*|tail -1` /Archive; mv `ls -tr P84C.*|tail -1` /process/process.dat)
0
 

Author Comment

by:cyberonics
ID: 7113604
bira,
One more thing. what if there are multiple files of

P84C.126, P84C.127
Then is there a way to say exception or something!

Thanks again!
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 6

Expert Comment

by:bira
ID: 7113819
If there are multiple files, it will process all of them.
  But only the last will remain as process.dat, because
  the name is the same.
    you could fix it by placing another name instead of
   process.dat.

      it could be  process.dat.`date +%d%m`
 
0
 

Author Comment

by:cyberonics
ID: 7113853
bira,
One more thing. what if there are multiple files of

P84C.126, P84C.127
Then is there a way to say exception or something!

Thanks again!
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7115850
multiple files: already solved, try my suggestion :-))
0
 
LVL 2

Accepted Solution

by:
JYoungman earned 50 total points
ID: 7116335
If you want to signal a problem if there is more than one file, do this :-


indir=/Incoming
archdir=/Archive
procdir=/process
destname="$procdir"/process.dat

set -- ${indir}/P84C.*
if test $# -gt 1
then
   # More than one file - issue error message and exit
   echo "More than one file has accumulated in ${indir}" >&2
   exit 1
fi

# Move the new file
if cp "$1" "$destname"
then
   if mv "$1" "$archdir"
   then
       # All OK.
       exit 0
   else
       # Failed to archive the file
       echo "Failed to archive file $i" >&2
       exit 1
   fi
else
   # Failed to copy the file into the processing directory.
   echo "Failed to copy $1 to $destname" >&2
   exit 1
fi



It might be better also to signal an error if /process/process.dat still exists when we want to copy a new one over it (this may mean that the previous file has not been processed yet).  However, you didn't ask for that.
0
 

Author Comment

by:cyberonics
ID: 7121817
Bira & JYoungman,
you guys were both great. Thanks for your help all

Thanks so much
cyberonics
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

816 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now