Solved

Rename a changing file name in UNIX?

Posted on 2002-06-26
8
448 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
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 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.

737 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