In a particular dir there are alot of files from today back to 1999 or earlier.
So I am trying to have the script search this dir and sort the files by create date of the file and put in dirs by year and the only data iw ant left in the source dir is this months only and when moving to next month i want only that month and so forth. .
for e.g is the create date has a year of 2004 I want it to put the file in a folder called 2004 if the folder does not exist create the folder and move the file there.
The other part in unix when you run ls -l , this is what i am using to create the array then split by spaces.
So when the files are create by this year. Sometimes in the year column it would show the time instead meaning it was created this year.
So now I want to break it dow where i am comparing the system month with month of the file and if they are the same and time is in that column leave the files the current dir. Now if the time is in column and the month is not the same put in the folder of that year. So when i move to another month only the files fromt hat month will stay and the rest are moved to the year folders.
This is what created. The problem i am getting is the files are moved to the destination folder where the year folders are suppose to be created. they are created but the files are not going in the folders and the source folder is gone after i runt he script. Ic hecked my paths and i am not seeing anything wrong. Hopefully somebody could help me please.
Here is an example how two records look on the screen. when i run the ls -l command.
-rw-r----- 1 bridge busr 1717 Oct 30 15:01 file2.txt
-rw-r----- 1 bridge ausr 1384 Mar 2 2004 file1.txt
Expand|Select|Wrap|Line Numbers
- #!/usr/bin/perl
- use File::Copy;
- ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
- $logdateTime = sprintf("%02d-%02d-%04d %02d:%02d:%02d", $mon + 1, $mday, $year + 1900, $hour, $min, $sec);
- $logdateTime2 = sprintf("%02d-%02d-%04d", $mon + 1, $mday, $year + 1900, $hour, $min, $sec);
- # Get Current Date Time
- @datepcs = split(/\s+/, scalar localtime);
- #printf("%s %s", @datepcs[1,2]);
- $dateTime = "@datepcs[1]";
- print $dateTime;
- #Source and Destination paths to change
- $sourceack = '/home/source;
- $ackdest = '/home/destination;
- #Run the date command to parse to get the year.
- $yearcmd = "date |";
- open (CMD, "$yearcmd") || die " Unable to run date command $!";
- @dd = <CMD>;
- $system_year = "$dd_[5]"; # Year showing up on the UNIX system
- chomp($system_year);
- close(CMD);
- chdir("$sourceack");
- $test = "ls -l |";
- open (INFO, "$test") || die " Unable to run list command $!";
- @rows = <INFO>;
- foreach (@rows) {
- $_ = split; # Split the line output in elements
- $year ="$_[7]"; # element to show the year
- chomp($year);
- $month = "$_[5]";
- chomp($month);
- print $year ."\n";
- if ("$year" =~ m/\:/) {
- #Checking for files for this month
- if ("$year" =~ m/\:/ && ($month == $dateTime)) {}
- #Check for files for this year only
- elsif (-d "$ackdest/$system_year") {
- move("$sourceack/$_[8]", "$ackdest/$system_year");
- }
- else {
- # Create new dir if it does not exist
- mkdir("$ackdest/$system_year") or die "Can't open $srcdir: $!";
- move("$sourceack/$_[8]", "$ackdest/$system_year");
- }
- }
- else {
- # IF the directory exists
- if (-d "$ackdest/$year") {
- move("$sourceack/$_[8]", "$ackdest/$year");
- }
- else {
- # Create new dir if it does not exist
- mkdir("$ackdest/$year") or die "Can't open $srcdir: $!";
- # Check if directory was created now
- if (-d "$ackdest/$_[7]") {
- move("$sourceack/$_[8]", "$ackdest/$_[7]");
- }
- }
- }
- } # close loop
- close(INFO);