?
Solved

Rename a changing file name in UNIX?

Posted on 2002-06-26
8
Medium Priority
?
451 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

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…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
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

764 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