copying files and Renaming them + shell script

Hi,

I have a problem.

I have some text files in a folder. The names can be like:

  emp.txt
  emp1.txt
  emp3.txt
 32emp4.txt

What i need is i have to copy all the files which have "emp" string in their filename
to a different folder and those file names should be appended by "date and timestamp" before the .txt

Can any one tell a shell script for this?

Example

All the above txt files should be renamed as

emp"Date and TimeStamp".txt
emp1"Date and TimeStamp".txt
emp3"Date and TimeStamp".txt
32emp4"Date and TimeStamp".txt

Thanks in advance....

Its always good if you have a starting script to work on. You learn better.

#! /bin/sh

for i in `ls *emp*.txt`
do
mv $i path/to/new/folder/$i$(date +%F-%T).txt
done

Not tested.

Vino

Thanks Vino.

It worked.

It did ? Didnt you get the .txt twice ? :confused:

Vino

Hi Vino,

I got the .txt twice but for my problem i think it should be fine.

If you have any way to remove the .txt before the date let me know.

I can adjust with the code sent by you....

Thanks.....

This should do it.

#! /bin/sh

for i in `ls *.cpp`
do
tmp=`echo $i | awk -F"." '{ print $1 }'`
mv $tmp.cpp $tmp$(date +%F-%T).cpp
done

Vino

Thanks Vino.........

This one worked perfectly fine......

Hi,

I have 5 files in a directory.

emp1_usage.txt
emp2_usage.txt
emp3_usage.txt
emp4_usage.txt
emp5_usage.txt

I am using sqlldr to get the contents of the above 5 files and store it in a temp table and update my original table using temp table.

for f in \*emp*.txt

do
sqlldr usr/passwd control=data.ctl data=$f
done

Now, the problem i got is:

I have a table called 'emdc' in which one column has the values:

emp_name
------------
emp1
emp2
emp3

I should dynamically check what are the values in the emdc table and pass to the sqlldr those file-names which have the values in the emdc table.

Let me explain Clearly:

I should now process only emp1_usage.txt, emp2_usage.txt, emp3_usage.txt into the sqlloader.

The script should automatically check for the values in emdc table and process only the 3 files among the 5 files present in the directory.

Please send me the snippet of the code.

Thanks in advance...........