bakerg1
asked on
Linux script to evaluate .txt files by month/year and move to another folder.
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?
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
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
ASKER
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.
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/$fil e_year/ ]; then
mkdir /PATH/TO/YEARS/FOLDER/$fil e_year/
fi
mv $f /PATH/TO/YEARS/FOLDER/$fil e_year/
done
for f in `ls`
file_year=`date --reference=$f +%y`
if [ ! -d /PATH/TO/YEARS/FOLDER/$fil
mkdir /PATH/TO/YEARS/FOLDER/$fil
fi
mv $f /PATH/TO/YEARS/FOLDER/$fil
done
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
That worked perfectly, Thanks!
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