Find the answer to your Linux question:
Results 1 to 5 of 5
I have a for loop that I would like to be able to specifiy a range of files untangle-20130214-0443.tar.gz untangle-20130215-0443.tar.gz untangle-20130216-0443.tar.gz untangle-20130217-0443.tar.gz files have the date in then but how ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2013
    Posts
    2

    trying to get a range in for command


    I have a for loop that I would like to be able to specifiy a range of files

    untangle-20130214-0443.tar.gz
    untangle-20130215-0443.tar.gz
    untangle-20130216-0443.tar.gz
    untangle-20130217-0443.tar.gz

    files have the date in then but how would i select
    untangle-20130215-0443.tar.gz
    untangle-20130216-0443.tar.gz
    in the for loop below


    for z in *$1*.tar.gz
    do
    nums=${z:9:8}
    tar -zxvf $z
    mv ./scripts/csv/app_events.csv ./scripts/csv/$nums-app_events.csv
    mv ./scripts/csv/web_events.csv ./scripts/csv/$nums-web_events.csv
    mv ./scripts/csv/virus_events.csv ./scripts/csv/$nums-virus_events.csv
    done


    untangle-20130214-0443.tar.gz
    untangle-20130215-0443.tar.gz
    untangle-20130216-0443.tar.gz
    untangle-20130217-0443.tar.gz

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Hello and welcome!

    If I understand you right, you want to detect if the file matches one of those two particular filenames, while you are doing the loop?

    If so, then try something like this case statement:
    Code:
    #!/bin/bash
    files='
    untangle-20130214-0443.tar.gz
    untangle-20130215-0443.tar.gz
    untangle-20130216-0443.tar.gz
    untangle-20130217-0443.tar.gz
    '
    
    # loop thru all files
    for z in $files;do
    
      case $z in
        *20130215*) echo "file $z is cool" ;;
        *20130216*) echo "file $z is cool" ;;
        *)  echo "file $z is not so interesting"
      esac
    
      # you can put more processing here, that will be run on all files
    done
    Upon matching, you can have it do any commands you want, not just echo something. You could also just do a if-then-else code block, too.

  3. #3
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    238
    hi,

    with BASH
    Code:
    for f in *2013021{5,6}*; do : ; done
    Code:
    for f in *2013021{5..7}*; do : ; done

  4. $spacer_open
    $spacer_close
  5. #4
    Just Joined!
    Join Date
    Feb 2013
    Posts
    2
    Actually my code allready does that unpack.sh 201301 would pick all files for January, what I need it ro do is select a ranges like

    20130120 -> 20130215 would be select "20th jan 2013" to "15th feb 2013"

  6. #5
    Linux Newbie
    Join Date
    Nov 2012
    Posts
    238
    I'm pretty sure I don't understand your requirement, however from what I can get:
    I'd compare seconds from Epoch
    Code:
    e=$(date -d "$1" +%s)
    l=$(date -d "$2" +%s)
    for f in *
    do df="${f#*-}"; df="${df%%-*}"
       if test "$l" -le "$df" && test "$df" -le "$e"
       then
          : do your stuff
       fi
    done
    more or less

Posting Permissions

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