Solved

unix script

Posted on 2010-09-21
13
528 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 76

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 76

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 76

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

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

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

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 76

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 76

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
improve Shell script 5 137
Unix Question 19 40
Linux: disable vim auto-comment 7 64
How to Insert a File Using Text Editor 9 55
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…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
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.:

760 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

20 Experts available now in Live!

Get 1:1 Help Now