Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1035273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:31:05+00:00 2026-05-16T14:31:05+00:00

As a bash script rookie, detailed answers would be appreciated. :) I am trying

  • 0

As a bash script rookie, detailed answers would be appreciated. 🙂

I am trying to write a bash script that parses JPEG EXIF time stamps output from the exiv2 commmandline utility, which gives me typical output like:

Image timestamp : 2010:07:27 17:38:52

Is there a way to parse the time stamp so that its components, like year, month, day, hour, minute, second, go into respective variables?

Would sed/gawk be the way to go? If so, how? Or is some other way better?

This way, I can manipulate or mix and match them however I like.

Here is the script I’ve got so far:

#!/bin/bash

COUNT=0
SKIPPED=0
FILES=0 # number of files encountered

# declare variables for time stamp

YEAR=0
MONTH=0
DAY=0

HOUR=0
MINUTE=0
SECOND=0

for CURRENT_FILE in * # a for loop to go through all files in current directory
do
    if [ -f "$CURRENT_FILE" ] # see if CURRENT_FILE is a file
    then
        FILETYPE=$(file -b --mime-type "$CURRENT_FILE") # get file type
        if [[ $FILETYPE == image/jpeg ]] # see if CURRENT_FILE's mime is image/jpeg
        then
            ((COUNT++))
            echo "Processing file $COUNT: $CURRENT_FILE"
            exiv2 "$CURRENT_FILE" | grep timestamp >> list
        else
            ((SKIPPED++))
            echo "Skipping file $CURRENT_FILE....."
        fi
        ((FILES++))
    fi
done

echo "Encountered $FILES files"
echo "Processed $COUNT files"
echo "Skipped $SKIPPED files"

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T14:31:05+00:00Added an answer on May 16, 2026 at 2:31 pm

    Here is a pure bash solution for splitting the timestamp specification line:

    SPEC='Image timestamp : 2010:07:27 17:38:52'
    read X X YEAR MONTH DAY HOUR MINUTE SECOND <<<${SPEC//:/ }
    echo $YEAR
    echo $MONTH
    echo $DAY
    echo $HOUR
    echo $MINUTE
    echo $SECOND
    

    The solution above converts colons to spaces in the specification, splits it on whitespace, and puts each item in the respective variable.

    A solution involving awk, sed or Perl would be similar, implementing the timestamp splitting in one of those languages.

    I recommend the pure bash solution though, because it’s faster (doesn’t have to spawn a subprocess), and it doesn’t have external dependencies. Nowadays (compared to the Bourne Shell in the 1970s) most of the string and array manipulation can be done in bash itself, without having to fork and exec expr, tr, sed, awk, perl, cut etc.

    With Perl:

    SPEC='Image timestamp : 2010:07:27 17:38:52'
    read X X YEAR MONTH DAY HOUR MINUTE SECOND <<<$(perl -pe 'y@:@ @' <<<$SPEC)
    echo $YEAR
    echo $MONTH
    echo $DAY
    echo $HOUR
    echo $MINUTE
    echo $SECOND
    

    With tr:

    SPEC='Image timestamp : 2010:07:27 17:38:52'
    read X X YEAR MONTH DAY HOUR MINUTE SECOND <<<$(tr : ' ' <<<$SPEC)
    echo $YEAR
    echo $MONTH
    echo $DAY
    echo $HOUR
    echo $MINUTE
    echo $SECOND
    

    With sed:

    SPEC='Image timestamp : 2010:07:27 17:38:52'
    read X X YEAR MONTH DAY HOUR MINUTE SECOND <<<$(sed 's/:/ /g' <<<$SPEC)
    echo $YEAR
    echo $MONTH
    echo $DAY
    echo $HOUR
    echo $MINUTE
    echo $SECOND
    

    With AWK:

    SPEC='Image timestamp : 2010:07:27 17:38:52'
    read X X YEAR MONTH DAY HOUR MINUTE SECOND <<<$(awk '{gsub(/:/," ");print}' <<<$SPEC)
    echo $YEAR
    echo $MONTH
    echo $DAY
    echo $HOUR
    echo $MINUTE
    echo $SECOND
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a short bash script to complete a task that involves creating a
I have a bash script that creates a Subversion patch file for the current
I'm writing a bash script that needs to delete old files. It's currently implemented
I have a bash script that runs a simulation program written in Fortran 90,
I wrote a bash script in that i had a Value=09 and when I
I have a bash script that goes like this : # gets all relevant
I have a bash script that is invoked from a cron job which has
My bash script is getting two arguments with folders (that exist and everything). Inside
I use a bash script that dumps separate areas of a single database to
I have a bash script that tries to call pgrep with arguments (Over simplified):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.