Link to home
Start Free TrialLog in
Avatar of sunshine737
sunshine737

asked on

unix script

i have n number of files  in a directory(example below)

directory: dev/source/
capture_1.xml
capture_2.xml
capture_3.xml
..
..


i need to move the files with new names (append timestamp with milli seconds) to other directory   /dev/archive/
eg:
capture_1.xml is renamed to   Sourceile_<timestampWithMilliSeconds)



can someone provide a unix script?
Thanks
Avatar of arnold
arnold
Flag of United States of America image

find /dev/source | while read a; do
timestamp_with_miliseconds=`stat $a | grep 'Modify:' | sed -e 's/^.*\: //' -e 's/\-0800//' |awk ' { print $1"_"$2 } '`
mv $a "/dev/archive/$a_$timestamp_with_milliseconds.xml"
done

Note the -0800 is the timezone offset.

Modify can be changed depending on what you want.
run stat on a file and you will see the Access and Change options as well.

If you want to decouple the capture_1 and .xml it can be done.
Avatar of sunshine737
sunshine737

ASKER

i do have some other files in /dev/source/.   i just want to move the files with capture_*.xml only.
find /dev/source | grep 'capture.*\.xml' | while
any other simple way instead of the below line:

timestamp_with_miliseconds=`stat $a | grep 'Modify:' | sed -e 's/^.*\: //' -e 's/\-0800//' |awk ' { print $1"_"$2 } '`
you can use a perl script and use a UNIX timestamp format (number of seconds since epoch January 1st 1970 GMT.)
the same command stat('filename')[9] has the modify timestamp
http://perldoc.perl.org/functions/stat.html

Not sure why you want milliseconds as it seems that the creation stamp is in seconds.

Are you wanting the timestamp to be the current time of the modification time of the file?

If you want the modification time of the file in milliseconds, almost all *nix filesystems don't support this.
would it be simple without milli seconds?

if so, i am ok with that..
What is the problem with the above assignment line?  Is a perl script something you are familiar with?
The example provided is a quick and semi-simple way to do what you asked.


source:
directory: dev/source/
capture_1.xml
capture_2.xml
capture_3.xml
..
...

output:
in directory:  /dev/archive

20100721_093016_capture_1.xml
20100721_093017_capture_2.xml
20100721_093017_capture_3.xml


just the date,hours ,minutes,seconds is enough,as each file is already having unique filename (capture_<serialNumber>.xml)


sorry,for not providing the info initially.

Thanks
i tried the following. but the value from filenamewithoutDir is getting null.

find /dev/source | grep 'capture.*\.xml' |while read a; do
filenamewithoutdir=basename $a
newfileName=`date +%Y%m%d_%H%M%S`_$filenamewithoutdir
mv $newfileName /dev/archive
done


ASKER CERTIFIED SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You need to enclose the basedir $a in execution ticks ` i.e.
filenamewithoutdir=`basename $a`
arnold, you can hugely simpify your code
#!/usr/bin/perl
use POSIX 'stftime';

foreach my $file (<*>) {
   my $ts = strftime "%Y%m%d_%H%M%S",localtime((stat($file))[9]);
   rename $file, "/dev/archive/$ts$file" or warn "Could not rename $file $!\n";
}

Open in new window