Solved

unix script

Posted on 2010-09-21
13
533 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 78

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 78

Expert Comment

by:arnold
ID: 33730347
find /dev/source | grep 'capture.*\.xml' | while
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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 78

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 78

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 78

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 78

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AD Cleanup by EmployeeID 11 81
AWK: Pytthagoras bp script Part deux 22 50
Merging two files with Perl 5 59
umask commands 5 18
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…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
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…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

751 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