Solved

unix script

Posted on 2010-09-21
13
532 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 

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
 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

My previous tech tip, Installing the Solaris OS From the Flash Archive On a Tape (http://www.experts-exchange.com/articles/OS/Unix/Solaris/Installing-the-Solaris-OS-From-the-Flash-Archive-on-a-Tape.html), discussed installing the Solaris Operating S…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 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