Results 1 to 9 of 9

Thread: Bash Script to create a folder with current date in the name

  1. #1
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    681
    Distro
    Ubuntu 14.04 Trusty Tahr

    Bash Script to create a folder with current date in the name

    Hi Guys,

    I routinely move photos from a digital camera to a folder on a hard drive.
    I usually name the destination folder today's date. Example: 4-april-2013
    I was wondering is it possible to write a script that would make a folder and name it the current date?

    I'd really appreciate any advice. It seems that should be doable, but I don't know how.
    Last edited by GrouchyGaijin; April 4th, 2013 at 07:58 PM. Reason: Question answered
    Thank you,
    GG -----------
    GUI we don't need no stinking GUI: A collection of Bash scripts
    Mutt (email), cmus (music),Google Calendar and Google Tasks cli

  2. #2
    Join Date
    May 2010
    Location
    Tewkesbury uk
    Beans
    7,806
    Distro
    Ubuntu Development Release

    Re: Bash Script to create a folder with current date in the name

    Hi

    You could use an alias but put it ~/.bashrc

    Code:
    matthew-S206:/home/matthew/tmp % alias mkdate='mkdir "$(date)"'
    matthew-S206:/home/matthew/tmp % mkdate
    matthew-S206:/home/matthew/tmp % ls
    Thu Apr  4 19:13:03 BST 2013/
    matthew-S206:/home/matthew/tmp %
    You can change the format string date returns as well.

    Kind regards
    If you believe everything you read, you better not read. ~ Japanese Proverb

    If you don't read the newspaper, you're uninformed. If you read the newspaper, you're mis-informed. - Mark Twain

  3. #3
    Join Date
    Apr 2012
    Beans
    5,472

    Re: Bash Script to create a folder with current date in the name

    You can use the mkdir command with the result of the data command, e.g.

    Code:
    mkdir $(date '+%d-%b-%Y')
    Code:
    man date

  4. #4
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    681
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Bash Script to create a folder with current date in the name

    WOW thanks guys! That was quick.
    I actually did a bit of playing around came back to mark the thread as solved.
    I came up with a script to do what I wanted.

    Code:
    #!/bin/bash
    now=$(date +"%m_%d_%Y")
    mkdir /media/Elements/"Image Staging"/$now &&
    mv *.JPG *.jpg /media/Elements/"Image Staging"/$now
    Now I can just run the script to dump all of the photos my wife takes into a folder on my external drive.

    Thanks again for the really quick and helpful replies.
    Thank you,
    GG -----------
    GUI we don't need no stinking GUI: A collection of Bash scripts
    Mutt (email), cmus (music),Google Calendar and Google Tasks cli

  5. #5
    Join Date
    May 2010
    Location
    Tewkesbury uk
    Beans
    7,806
    Distro
    Ubuntu Development Release

    Re: Bash Script to create a folder with current date in the name

    Hi

    I am glad we were able to help you

    BTW: I would probably use a function instead of a script for something that small and keep it in ~/.bashrc.

    By doing that (well i keep them in a separate file and source it in ~/.bashrc), i have built up a library of functions and i don't have to think about where i have stored them.

    Kind regards
    If you believe everything you read, you better not read. ~ Japanese Proverb

    If you don't read the newspaper, you're uninformed. If you read the newspaper, you're mis-informed. - Mark Twain

  6. #6
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    681
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Bash Script to create a folder with current date in the name

    Quote Originally Posted by matt_symes View Post

    BTW: I would probably use a function instead of a script for something that small and keep it in ~/.bashrc.

    By doing that (well i keep them in a separate file and source it in ~/.bashrc), i have built up a library of functions and i don't have to think about where i have stored them.

    Kind regards
    That sounds like a good idea. Can you explain how to do so?
    (I just learned about variables - I'm not sure what how a function is written or where exactly to put one in the ~./bashrc file. I also don't know what is meant by source the function from another file.)

    Thanks again!
    Thank you,
    GG -----------
    GUI we don't need no stinking GUI: A collection of Bash scripts
    Mutt (email), cmus (music),Google Calendar and Google Tasks cli

  7. #7
    Join Date
    May 2010
    Location
    Tewkesbury uk
    Beans
    7,806
    Distro
    Ubuntu Development Release

    Re: Bash Script to create a folder with current date in the name

    Hi

    Open a terminal and type

    Code:
    nano ~/.bashrc
    At the bottom of the file add this.

    Code:
    function mkdate
    {
            now=$(date +"%m_%d_%Y");
            mkdir -p /media/Elements/"Image Staging"/$now &&
            mv *.JPG *.jpg /media/Elements/"Image Staging"/$now
    }
    You can source the file but it's easier if you just reboot your PC. This will ensure your ~/.bashrc file is re-read and you will have access to the function mkdate.

    At the terminal you should just be able to type

    Code:
    mkdate
    This will call that function, creating the directory with the date (make sure you have permissions to do this on the drive) and then copy the jpeg files in your current directory into the directory just created.

    Incedently, i have used mkdir with the -p switch.

    Kind regards
    If you believe everything you read, you better not read. ~ Japanese Proverb

    If you don't read the newspaper, you're uninformed. If you read the newspaper, you're mis-informed. - Mark Twain

  8. #8
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    681
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Bash Script to create a folder with current date in the name

    Hey, thanks a lot!
    What does the -p switch do?
    Thank you,
    GG -----------
    GUI we don't need no stinking GUI: A collection of Bash scripts
    Mutt (email), cmus (music),Google Calendar and Google Tasks cli

  9. #9
    Join Date
    May 2010
    Location
    Tewkesbury uk
    Beans
    7,806
    Distro
    Ubuntu Development Release

    Re: Bash Script to create a folder with current date in the name

    Hi

    mkdir wil fail if all the directories in the path you are creating do not exist.

    The -p switch will ensure it creates all the directories in the path and so not fail.

    Take a look

    Code:
    matthew-S206:/home/matthew % sudo mkdir /media/test1/test2
    [sudo] password for matthew: 
    mkdir: cannot create directory `/media/test1/test2': No such file or directory
    matthew-S206:/home/matthew % sudo mkdir -p /media/test1/test2
    matthew-S206:/home/matthew % ls /media/test1 
    test2/
    matthew-S206:/home/matthew %
    Kind regards
    If you believe everything you read, you better not read. ~ Japanese Proverb

    If you don't read the newspaper, you're uninformed. If you read the newspaper, you're mis-informed. - Mark Twain

Tags for this Thread

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
  •