Solved

unix script

Posted on 2010-09-21
13
529 Views
Last Modified: 2013-12-26
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
0
Comment
Question by:sunshine737
  • 6
  • 5
  • 2
13 Comments
 
LVL 77

Expert Comment

by:arnold
ID: 33729686
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.
0
 

Author Comment

by:sunshine737
ID: 33729908
i do have some other files in /dev/source/.   i just want to move the files with capture_*.xml only.
0
 
LVL 77

Expert Comment

by:arnold
ID: 33730347
find /dev/source | grep 'capture.*\.xml' | while
0
 

Author Comment

by:sunshine737
ID: 33730602
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 } '`
0
 
LVL 77

Expert Comment

by:arnold
ID: 33731444
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.

0
 
LVL 48

Expert Comment

by:Tintin
ID: 33732460
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:sunshine737
ID: 33734726
would it be simple without milli seconds?

if so, i am ok with that..
0
 
LVL 77

Expert Comment

by:arnold
ID: 33734813
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.
0
 

Author Comment

by:sunshine737
ID: 33734825


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
0
 

Author Comment

by:sunshine737
ID: 33735115
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


0
 
LVL 77

Accepted Solution

by:
arnold earned 500 total points
ID: 33735281
you would need to use perl or process the data
The alternative is to use the perl script to build the timestamp.
remove the while() { and the closing }
The open/close
Put
$_=$ARGV[0];
replace the rename with print

such that you within the shell script have
$timestamp=`perlscript $filename`;
Note you should use the full path for the filename.
#!/usr/bin/perl
open DIR,"ls|";
while (<DIR>) {
chomp();
@date_of_modification=localtime((stat("$_"))[9]);
$year=$date_of_modification[5]+1900;
$mon=1+$date_of_modification[4];
$mon = ($mon<=9) ? "0".$mon: $mon;
$day=$date_of_modification[3]; 
$day= ($day<9) ? "0".$day :$day;
$hour=$date_of_modification[2];
$min=$date_of_modification[1];
$sec=$date_of_modification[0];
$min = ($min<10) ? "0".$min : $min;
$hour= ($hour<10) ? "0".$hour:$hour;
$sec= ($sec<10) ? "0".$sec:$sec;
$timestamp="$year$mon$day"."_"."$hour$min$sec";
rename ($_,"/dev/archive/$timestamp"."_"."$_");
}
close(DIR)

Alternative: as script that will output the timestamp for a file provided on the command line.  note this does not check whether an argument is being passed
#!/usr/bin/perl
$_=$ARGV[0];
chomp();
@date_of_modification=localtime((stat("$_"))[9]);
$year=$date_of_modification[5]+1900;
$mon=1+$date_of_modification[4];
$mon = ($mon<=9) ? "0".$mon: $mon;$day=$date_of_modification[3]; 
$day= ($day<9) ? "0".$day :$day;$hour=$date_of_modification[2];
$min=$date_of_modification[1];
$sec=$date_of_modification[0];
$min = ($min<10) ? "0".$min : $min;
$hour= ($hour<10) ? "0".$hour:$hour;
$sec= ($sec<10) ? "0".$sec:$sec;
$timestamp="$year$mon$day"."_"."$hour$min$sec";
print "$timestamp\n";

Open in new window

0
 
LVL 77

Expert Comment

by:arnold
ID: 33735295
You need to enclose the basedir $a in execution ticks ` i.e.
filenamewithoutdir=`basename $a`
0
 
LVL 48

Expert Comment

by:Tintin
ID: 33749943
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

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

910 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

24 Experts available now in Live!

Get 1:1 Help Now