Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

unix script

Posted on 2010-09-21
13
Medium Priority
?
535 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
13 Comments
 
LVL 79

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 79

Expert Comment

by:arnold
ID: 33730347
find /dev/source | grep 'capture.*\.xml' | while
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 79

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 79

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 79

Accepted Solution

by:
arnold earned 2000 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 79

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
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…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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…
Suggested Courses

670 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