Solved

Linux script to evaluate .txt files by month/year and move to another folder.

Posted on 2012-04-05
7
666 Views
Last Modified: 2012-04-11
I hope there is a way to accomplish this task.  I have several .txt files that I need to move to another folder that contains folders  by year, i.e. "2011", "2012", etc.  The problem is that we run on a fiscal calendar that does not coincide with the regular calendar.  I want to run a simple script that can scan the month and year of each file and move the file to the appropriate year's folder.  The fiscal calendar runs February 1st through January 31st, how do I compare the file's month/year to the fiscal calendar to ensure the files are placed properly?
0
Comment
Question by:bakerg1
  • 3
  • 3
7 Comments
 
LVL 28

Expert Comment

by:serialband
ID: 37814087
You could touch two files with the dates in question and find all files between those dates.  The first file would be dated 2/1/2011 at midnight and the 2nd would be dated /1/31/2012 at midnight.

touch -t 201102010000 first
touch -t 201201310000 last

Then you use the find command to find files between those dates.

find / -newer first ! -newer last
0
 
LVL 14

Expert Comment

by:kronostm
ID: 37815662
not sure what's with the fiscal calendar, but you might try testing with something like this

for f in `ls`
  file_year=`date --reference=$f +%y`
  if [ $file_year -eq 2011 ]; then
     mv $f /PATH_TO-YEARS/2011/
  elif [ $file_year -eq 2012 ]; then
     mv  $f /PATH_TO-YEARS/2012/
     .......
  fi
done
0
 

Author Comment

by:bakerg1
ID: 37816453
Thanks for the advice, I think I will try to blend the two suggestions and see how I can make them work, i will let you know.  I want to figure out a way to use a variable for the year that will enable me to drop files into the appropriate folder without having to add to the script when the year changes.  Still open to other suggestions.
0
Network it in WD Red

There's an industry-leading WD Red drive for every compatible NAS system to help fulfill your data storage needs. With drives up to 8TB, WD Red offers a wide array of solutions for customers looking to build the biggest, best-performing NAS storage solution.  

 
LVL 14

Expert Comment

by:kronostm
ID: 37822521
for every file in current directory, get it's year in a variable, then check if the folder with the named year exists, create if it doesn't, move the file to the folder and live happily ever after :)

for f in `ls`
  file_year=`date --reference=$f +%y`
  if [ ! -d /PATH/TO/YEARS/FOLDER/$file_year/ ]; then
     mkdir /PATH/TO/YEARS/FOLDER/$file_year/
  fi
  mv $f /PATH/TO/YEARS/FOLDER/$file_year/
done
0
 

Author Comment

by:bakerg1
ID: 37830437
kronostm,
For the line of code: file_year=`date --reference=$f +%y`
I keep getting the following error: "syntax error near unexpected token `+%Y`"

I have tried several variations and it keeps returning this error.  Does the format %Y work with the --reference?  I cannot find any documentation describing what you have here.  This solution will work, I have gotten the ok to sort strictly by calendar year, I just need to isolate the year for each file.  Thanks for the help.
0
 
LVL 14

Accepted Solution

by:
kronostm earned 500 total points
ID: 37831278
I'm sorry, I wrote that initial script without testing, I forgot a "do", my bad. Below is the tested script:

#!/bin/bash
for f in `ls`; do
  file_year=`date --reference=$f +%Y`
  if [ ! -d /PATH/$file_year/ ]; then
     mkdir /PATH/$file_year/
  fi
  mv $f /PATH/$file_year/
done
0
 

Author Comment

by:bakerg1
ID: 37832701
That worked perfectly, Thanks!
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Remove a very long string from all files in Linux 6 87
AWS CLI - Instances, Volumes & Tagging 2 74
AWS CLI - Issue with name display 2 51
php ssh2_scp_send 1 49
In my business, I use the LTS (Long Term Support) versions of Linux. My workstations do real work, and so I rarely have the patience to deal with silly problems caused by an upgraded kernel that had experimental software on it to begin with from a r…
1. Introduction As many people are interested in Linux but not as many are interested or knowledgeable (enough) to install Linux on their system, here is a safe way to try out Linux on your existing (Windows) system. The idea is that you insta…
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.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

910 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

16 Experts available now in Live!

Get 1:1 Help Now