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_<timestampWithMi lliSeconds )
can someone provide a unix script?
Thanks
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_<timestampWithMi
can someone provide a unix script?
Thanks
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
ASKER
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 } '`
timestamp_with_miliseconds
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.
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.
If you want the modification time of the file in milliseconds, almost all *nix filesystems don't support this.
ASKER
would it be simple without milli seconds?
if so, i am ok with that..
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.
The example provided is a quick and semi-simple way to do what you asked.
ASKER
source:
directory: dev/source/
capture_1.xml
capture_2.xml
capture_3.xml
..
...
output:
in directory: /dev/archive
20100721_093016_capture_1.
20100721_093017_capture_2.
20100721_093017_capture_3.
just the date,hours ,minutes,seconds is enough,as each file is already having unique filename (capture_<serialNumber>.xm
sorry,for not providing the info initially.
Thanks
ASKER
i tried the following. but the value from filenamewithoutDir is getting null.
find /dev/source | grep 'capture.*\.xml' |while read a; do
filenamewithoutdir=basenam e $a
newfileName=`date +%Y%m%d_%H%M%S`_$filenamew ithoutdir
mv $newfileName /dev/archive
done
find /dev/source | grep 'capture.*\.xml' |while read a; do
filenamewithoutdir=basenam
newfileName=`date +%Y%m%d_%H%M%S`_$filenamew
mv $newfileName /dev/archive
done
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You need to enclose the basedir $a in execution ticks ` i.e.
filenamewithoutdir=`basena me $a`
filenamewithoutdir=`basena
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";
}
timestamp_with_miliseconds
mv $a "/dev/archive/$a_$timestam
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.