Results 1 to 3 of 3
I am trying to occasionally move some files according to their filename from one folder to another utilizing a bash script. Unfortunately, there are a couple of catches to this.
...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 02-17-2011 #1Just Joined!
- Join Date
- Apr 2005
- Posts
- 37
[SOLVED] Another Current Date File Move Problem
I am trying to occasionally move some files according to their filename from one folder to another utilizing a bash script. Unfortunately, there are a couple of catches to this.
Catch #1 is that all these files were named according to date and all files that include the current date are to be excluded from the move.
Catch #2 is that mtime can't be used. These files are subject to being accessed and modified at any time prior to the script running.
I've included a short file list below as an example. They are always going to be in a xx-YEAR-MONTH-DAY.txt format.
cp-2011-02-15.txt
df-2011-02-17.txt
gg-2010-12-23.txt
gt-2009-11-30.txt
mp-2011-02-17.txt
mp-2011-02-15.txt
mf-2009-11-30.txt
vm-2011-01-04.txt
Saying that today's date is 2011-02-17, I need to end up with:
df-2011-02-17.txt
mp-2011-02-17.txt
in my current folder /home/ned/.mercdat and the remainder need to be moved to /home/ned/mercarc
I can do this easily in dos batch but, for the life of me, I don't even come close in bash. I can do it utilizing mtime but never get desired results. I'm always ending up with several older dated files in the source folder.
Any clues concerning how to go about accomplish this? Perhaps a variable formulated from the current date needs to be involved?
- 02-17-2011 #2
This should work
If that produces a agreeable filelist, replaceCode:TODAY=`date +%Y-%m-%d` ; find /home/ned/.mercdat/ -type f ! -name "*-$TODAY.txt" -print0 | while read -r -d '' FILE; do ls ${FILE} ; done
Other than that:Code:ls ${FILE} with mv ${FILE} /home/ned/mercarc/
there is hardcoding
and no errorhandling at all,
so you might want to rework that a little bit before it is used on production.You must always face the curtain with a bow.
- 02-18-2011 #3Just Joined!
- Join Date
- Apr 2005
- Posts
- 37
Irithori,
Your script works as desired. I was close but I see where I was going astray, especially with -type f and -print0
I can see where some things could make the script fail and I'll work on preventing that but overall it's always going to be the same scenario and the files are application generated files that will always have the same name format and will never include spaces, etc. unless someone alters the output format in the prog or renames files after the fact. In this case the only person that would be doing that is me so I am not going to be doing much adjusting in this case.
I do appreciate your help with this. Mtime was a headache in this situation and I knew there'd be a logical resolve for it. Between trying to work it out for myself an googling I spent several frustrating days trying to get something to work specifically the way I needed it too. You've obviously resolved that in short order. Thank you.



