Find the answer to your Linux question:
Results 1 to 3 of 3
I am trying to rename multiple files in a directory and add the Date/Time stamp to the file name. Example: file1.txt file2.txt file3.txt Renamed to 2009_02_01_21.txt 2009_02_02_21.txt 2009_02_03_21.txt I am ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Feb 2009
    Posts
    4

    Unhappy Need Help renaming multiple files


    I am trying to rename multiple files in a directory and add the Date/Time stamp to the file name.

    Example:
    file1.txt
    file2.txt
    file3.txt

    Renamed to
    2009_02_01_21.txt
    2009_02_02_21.txt
    2009_02_03_21.txt

    I am able to accomplish this using this command:
    mv file1.txt "`date +%Y_%m%d_%H`".txt

    But I want to automate this task, I have many files to process and I dont want to do them one by one.

    I already tried this but it does not work
    mv *.txt "`date +%Y_%m%d_%H`".txt

    Thanks in advance for any help.

  2. #2
    Linux Guru bigtomrodney's Avatar
    Join Date
    Nov 2004
    Location
    Ireland
    Posts
    6,133
    I'm assuming you want to take the number from the original filename and put it in the third number position. This is quick and hackish, but it might get you started:
    Code:
     for i in *;do mv "$i" "$(date +%Y_%m_$(echo $i |sed s/file// |sed s/.txt//)_%d)".txt;done

  3. #3
    Just Joined!
    Join Date
    Feb 2009
    Posts
    4

    Smile

    Quote Originally Posted by bigtomrodney View Post
    I'm assuming you want to take the number from the original filename and put it in the third number position. This is quick and hackish, but it might get you started:
    Code:
     for i in *;do mv "$i" "$(date +%Y_%m_$(echo $i |sed s/file// |sed s/.txt//)_%d)".txt;done
    BigTomRodney

    Thanks for the help what would I use if I did not want to kee the original filename just the Date/Time stamp?

    Also your solution does not handle spaces well.

    J

  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
  •