?
Solved

Rename a changing file name in UNIX?

Posted on 2002-06-26
8
Medium Priority
?
456 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
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 how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

749 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