Solved

Rename a changing file name in UNIX?

Posted on 2002-06-26
8
444 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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.

708 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

18 Experts available now in Live!

Get 1:1 Help Now