Results 1 to 9 of 9

Thread: Use info from file name to rename file

  1. #1
    Join Date
    Jun 2010
    Location
    Portugal
    Beans
    337
    Distro
    Ubuntu 10.04 Lucid Lynx

    Use info from file name to rename file

    I've a huge amount of file that are named like A2012178.1220.051.2012178233531.hdf There, from the 2nd character to the 8th is the date, year plus julian day. From the 13rd character to the extention of the file the name is rubbish to me... so I would like to rename the files and convert the date (for convenience).
    So far I'm able to convert the date in bash
    Code:
    CONVERTED=$(date -d "${Year}-01-01 +${JulianDay} days -1 day" "+%Y%m%d")

    But I have no idea of how to read the year and julian day from the file name and replace the name within the bash script....
    Any idea?
    Last edited by JCM_Pico; March 28th, 2013 at 12:40 PM.

  2. #2
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Use info from file name to rename file

    Code:
    file='A2012178.1220.051.2012178233531.hdf'
    year="${file:1:4}"
    tail="${file:5}"
    jday="${tail%%.*}"
    BTW, the last six digits before extension look like time:
    Code:
    $ echo ${file: -10:6}
    233531
    Last edited by schragge; March 27th, 2013 at 07:29 PM.

  3. #3
    Join Date
    Jun 2010
    Location
    Portugal
    Beans
    337
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Use info from file name to rename file

    Nice ... it works =)

    Do you know how can i give the list of files contained within a folder to give to the script? for example to use as the i variable in a for loop?

  4. #4
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Use info from file name to rename file

    It is pretty staightforward
    Code:
    for file in path/to/folder/*.hdf; do
     echo $file
    done

  5. #5
    Join Date
    Jun 2010
    Location
    Portugal
    Beans
    337
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Use info from file name to rename file

    Not working as I would expect =( .... extra help?

    This is my code
    Code:
    #!/bin/bash
    
    PathToFiles='TEST/'
    if [ $# -lt 1 ]; then
      echo "Usage: $0 <Path for folder containing the .hdf files> "
      exit 1
    fi
    
    
    echo $PathToFiles
    
    
    for file in "${PathToFiles}*.hdf"; do
    
    
      echo $file
    
    
      Year=${file:12:4}
      JulianDay=${file:16:3}
      Hour=${file:20:4}
    
    
      CONVERTED=$(date -d "${Year}-01-01 +${JulianDay} days -1 day" "+%Y%m%d")
    
    
      echo $year
    
    
      mv ${file} "${PathToFiles}MODIS_${CONVERTED}.hdf"
    
    
    done
    This is the output
    Code:
    ./Converter.sh TEST/TEST/
    TEST/MOD04_L2.A2012178.0200.051.2012178231101.hdf TEST/MOD04_L2.A2012178.0845.051.2012178232943.hdf TEST/MOD04_L2.A2012178.0850.051.2012178233011.hdf TEST/MOD04_L2.A2012178.0855.051.2012178233107.hdf TEST/MOD04_L2.A2012178.0900.051.2012178233042.hdf TEST/MOD04_L2.A2012178.0905.051.2012178233123.hdf TEST/MOD04_L2.A2012178.0910.051.2012178233159.hdf TEST/MOD04_L2.A2012178.1025.051.2012178233314.hdf TEST/MOD04_L2.A2012178.1030.051.2012178233342.hdf TEST/MOD04_L2.A2012178.1035.051.2012178233312.hdf TEST/MOD04_L2.A2012178.1040.051.2012178233259.hdf TEST/MOD04_L2.A2012178.1045.051.2012178233416.hdf TEST/MOD04_L2.A2012178.1200.051.2012178233606.hdf TEST/MOD04_L2.A2012178.1205.051.2012178233523.hdf TEST/MOD04_L2.A2012178.1210.051.2012178233538.hdf TEST/MOD04_L2.A2012178.1215.051.2012178234202.hdf TEST/MOD04_L2.A2012178.1220.051.2012178233531.hdf TEST/MOD04_L2.A2012178.1225.051.2012178233546.hdf TEST/MOD04_L2.A2012178.1340.051.2012178233503.hdf TEST/MOD04_L2.A2012178.1345.051.2012178234132.hdf TEST/MOD04_L2.A2012178.1350.051.2012178234118.hdf TEST/MOD04_L2.A2012178.1355.051.2012178234137.hdf TEST/MOD04_L2.A2012178.1400.051.2012178234134.hdf TEST/MOD04_L2.A2012178.1405.051.2012178234148.hdf TEST/MOD04_L2.A2012178.1515.051.2012179013419.hdf TEST/MOD04_L2.A2012178.1520.051.2012179014757.hdf TEST/MOD04_L2.A2012178.1525.051.2012179014127.hdf TEST/MOD04_L2.A2012178.1530.051.2012179014105.hdf TEST/MOD04_L2.A2012178.1535.051.2012179013427.hdf TEST/MOD04_L2.A2012178.1650.051.2012179013418.hdf TEST/MOD04_L2.A2012178.1655.051.2012179014056.hdf TEST/MOD04_L2.A2012178.1700.051.2012179013430.hdf TEST/MOD04_L2.A2012178.1830.051.2012179014106.hdf TEST/MOD04_L2.A2012178.1835.051.2012179014338.hdf TEST/MOD04_L2.A2012178.2010.051.2012179035835.hdf TEST/MOD04_L2.A2012178.2015.051.2012179035529.hdf TEST/MOD04_L2.A2012178.2150.051.2012179074631.hdf TEST/MOD04_L2.A2012178.2325.051.2012179074258.hdf TEST/MOD04_L2.A2012178.2330.051.2012179074630.hdf TEST/MOD04_L2.A2012179.0105.051.2012179222900.hdf TEST/MOD04_L2.A2012179.0245.051.2012179222144.hdf TEST/MOD04_L2.A2012179.0750.051.2012179222322.hdf TEST/MOD04_L2.A2012179.0755.051.2012179221227.hdf TEST/MOD04_L2.A2012179.0800.051.2012179221525.hdf TEST/MOD04_L2.A2012179.0805.051.2012179221158.hdf TEST/MOD04_L2.A2012179.0810.051.2012179222509.hdf TEST/MOD04_L2.A2012179.0815.051.2012179221447.hdf TEST/MOD04_L2.A2012179.0930.051.2012179221213.hdf TEST/MOD04_L2.A2012179.0935.051.2012179221507.hdf TEST/MOD04_L2.A2012179.0940.051.2012179221846.hdf TEST/MOD04_L2.A2012179.0945.051.2012179221354.hdf TEST/MOD04_L2.A2012179.0950.051.2012179221109.hdf TEST/MOD04_L2.A2012179.1105.051.2012179220943.hdf TEST/MOD04_L2.A2012179.1110.051.2012179221505.hdf TEST/MOD04_L2.A2012179.1115.051.2012179220918.hdf TEST/MOD04_L2.A2012179.1120.051.2012179221436.hdf TEST/MOD04_L2.A2012179.1125.051.2012179221200.hdf TEST/MOD04_L2.A2012179.1130.051.2012179222737.hdf TEST/MOD04_L2.A2012179.1245.051.2012179221223.hdf TEST/MOD04_L2.A2012179.1250.051.2012179222703.hdf TEST/MOD04_L2.A2012179.1255.051.2012179222340.hdf TEST/MOD04_L2.A2012179.1300.051.2012179222158.hdf TEST/MOD04_L2.A2012179.1305.051.2012179221221.hdf TEST/MOD04_L2.A2012179.1310.051.2012179221611.hdf TEST/MOD04_L2.A2012179.1420.051.2012179221101.hdf TEST/MOD04_L2.A2012179.1425.051.2012179220959.hdf TEST/MOD04_L2.A2012179.1430.051.2012179221220.hdf TEST/MOD04_L2.A2012179.1435.051.2012179222422.hdf TEST/MOD04_L2.A2012179.1440.051.2012179220956.hdf TEST/MOD04_L2.A2012179.1445.051.2012179221444.hdf TEST/MOD04_L2.A2012179.1450.051.2012179222541.hdf TEST/MOD04_L2.A2012179.1555.051.2012180020423.hdf TEST/MOD04_L2.A2012179.1600.051.2012180015944.hdf TEST/MOD04_L2.A2012179.1605.051.2012180015530.hdf TEST/MOD04_L2.A2012179.1610.051.2012180015617.hdf TEST/MOD04_L2.A2012179.1735.051.2012180015112.hdf TEST/MOD04_L2.A2012179.1740.051.2012180020427.hdf TEST/MOD04_L2.A2012179.1745.051.2012180015817.hdf TEST/MOD04_L2.A2012179.1915.051.2012180015122.hdf TEST/MOD04_L2.A2012179.1920.051.2012180020042.hdf TEST/MOD04_L2.A2012179.2055.051.2012180040117.hdf TEST/MOD04_L2.A2012179.2230.051.2012180074251.hdf TEST/MOD04_L2.A2012179.2235.051.2012180074809.hdf TEST/MOD04_L2.A2012180.0010.051.2012180222113.hdf TEST/MOD04_L2.A2012180.0150.051.2012180222958.hdf TEST/MOD04_L2.A2012180.0835.051.2012180222311.hdf TEST/MOD04_L2.A2012180.0840.051.2012180221509.hdf TEST/MOD04_L2.A2012180.0845.051.2012180223909.hdf TEST/MOD04_L2.A2012180.0850.051.2012180221920.hdf TEST/MOD04_L2.A2012180.0855.051.2012180221921.hdf TEST/MOD04_L2.A2012180.1010.051.2012180221715.hdf TEST/MOD04_L2.A2012180.1015.051.2012180221729.hdf TEST/MOD04_L2.A2012180.1020.051.2012180221659.hdf TEST/MOD04_L2.A2012180.1025.051.2012180222030.hdf TEST/MOD04_L2.A2012180.1030.051.2012180221903.hdf TEST/MOD04_L2.A2012180.1035.051.2012180221454.hdf TEST/MOD04_L2.A2012180.1150.051.2012180222057.hdf TEST/MOD04_L2.A2012180.1155.051.2012180221534.hdf TEST/MOD04_L2.A2012180.1200.051.2012180221633.hdf TEST/MOD04_L2.A2012180.1205.051.2012180222054.hdf TEST/MOD04_L2.A2012180.1210.051.2012180222953.hdf TEST/MOD04_L2.A2012180.1215.051.2012180221807.hdf TEST/MOD04_L2.A2012180.1325.051.2012180221449.hdf TEST/MOD04_L2.A2012180.1330.051.2012180222004.hdf TEST/MOD04_L2.A2012180.1335.051.2012180222007.hdf TEST/MOD04_L2.A2012180.1340.051.2012180221525.hdf TEST/MOD04_L2.A2012180.1345.051.2012180221913.hdf TEST/MOD04_L2.A2012180.1350.051.2012180221928.hdf TEST/MOD04_L2.A2012180.1355.051.2012180223037.hdf TEST/MOD04_L2.A2012180.1505.051.2012181014000.hdf TEST/MOD04_L2.A2012180.1510.051.2012181014516.hdf TEST/MOD04_L2.A2012180.1515.051.2012181014512.hdf TEST/MOD04_L2.A2012180.1520.051.2012181014003.hdf TEST/MOD04_L2.A2012180.1525.051.2012181014401.hdf TEST/MOD04_L2.A2012180.1640.051.2012181014516.hdf TEST/MOD04_L2.A2012180.1645.051.2012181014010.hdf TEST/MOD04_L2.A2012180.1650.051.2012181014127.hdf TEST/MOD04_L2.A2012180.1820.051.2012181014723.hdf TEST/MOD04_L2.A2012180.1825.051.2012181014749.hdf TEST/MOD04_L2.A2012180.1955.051.2012181035404.hdf TEST/MOD04_L2.A2012180.2000.051.2012181035411.hdf TEST/MOD04_L2.A2012180.2135.051.2012181080611.hdf TEST/MOD04_L2.A2012180.2140.051.2012181080605.hdf TEST/MOD04_L2.A2012180.2315.051.2012181080627.hdf
    date: invalid date `-01-01 + days -1 day'
    
    
    mv: target `TEST/MODIS_.hdf' is not a directory

  6. #6
    Join Date
    Jun 2010
    Location
    Portugal
    Beans
    337
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Use info from file name to rename file

    The problem was in the quotes when setting the file

    here is the final working script =)

    Code:
    #!/bin/bash
    
    PathToFiles=$1
    if [ $# -lt 1 ]; then
      echo "Usage: $0 <Path for folder containing the .hdf files> "
      exit 1
    fi
    
    
    cd $PathToFiles
    MOD04_L2.A2012178.0200.051.2012178231101.hdf
    for file in *.hdf; do
    
    
      echo $file
    
    
      Year=${file:10:4}
      JulianDay=${file:14:3}
      Hour=${file:18:4}
    
    
      CONVERTED=$(date -d "${Year}-01-01 +${JulianDay} days -1 day" "+%Y%m%d")
    
    
      echo $year
    
    
      mv ${file} "MODIS_${CONVERTED}_${Hour}.hdf"
    
    
    done
    ~                                                                                                                                                                            
    ~

  7. #7
    Join Date
    Jul 2010
    Location
    Kent, UK
    Beans
    15
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Use info from file name to rename file

    Not sure about the invalid date, but for the mv surely file needs TEST/ Appended?

    [EDIT] Guess I was wrong
    Last edited by hectorgrebbell; March 28th, 2013 at 11:29 AM.

  8. #8
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Use info from file name to rename file

    Quote Originally Posted by JCM_Pico View Post
    The problem was in the quotes when setting the file
    Yes. Still, quotes are good when used properly:
    Code:
    for file in "$PathToFiles"*.hdf; do
    Otherwise, your script won't work with paths containing spaces.

    This also applies to the line:
    Code:
    cd "$PathToFile"
    Note the difference between variable assignments and command arguments:
    Code:
    var1='a b' var2='c d'; var3=$var1 command arg=$var2
    is equivalent to
    Code:
    var3='a b' command arg=c d
    bash will perform word-splitting on the expanded value of $var2, but not on $var1, so if $var2 contains spaces it will be treated as two arguments (arg=c and d), whereas $var3 always will be assigned the whole value of $var1. Always quote arguments to a command if you don't want them to be word-splitted.
    Last edited by schragge; March 28th, 2013 at 12:47 PM.

  9. #9
    Join Date
    Jun 2010
    Location
    Portugal
    Beans
    337
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Use info from file name to rename file

    Thanks alot you both =)

Bookmarks

Posting Permissions

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