Find the answer to your Linux question:
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.
  1. #1
    Just Joined!
    Join Date
    Apr 2005
    Posts
    41

    [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?

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,411
    This should work
    Code:
    TODAY=`date +%Y-%m-%d` ;  find /home/ned/.mercdat/ -type f ! -name "*-$TODAY.txt" -print0 | while read -r -d '' FILE; do ls ${FILE}  ; done
    If that produces a agreeable filelist, replace
    Code:
    ls ${FILE}
    with
    mv ${FILE} /home/ned/mercarc/
    Other than that:
    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.

  3. #3
    Just Joined!
    Join Date
    Apr 2005
    Posts
    41
    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.

  4. $spacer_open
    $spacer_close

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •