Unix/Linux Go Back    

Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Adding date and time to file name

Shell Programming and Scripting

directory, multiple files, renaming a file

Thread Tools Search this Thread Display Modes
Old Unix and Linux 11-15-2010   -   Original Discussion by cc_at_work
frans's Unix or Linux Image
frans frans is offline Forum Advisor  
Registered User
Join Date: Oct 2009
Last Activity: 1 February 2016, 9:47 AM EST
Location: France
Posts: 839
Thanks: 4
Thanked 54 Times in 53 Posts
Something like
bash code:
  1. #!/bin/bash
  2. D=$(date +%d-%m-%Y)
  3. for F in *
  4. do
  5.    Dot="${F//[^\.]/}" # this removes anything but a dot
  6.    if [ -n "$Dot" ]; then
  7.       mv "$F" "${F%.*}-$D.${F##*.}"
  8.    else
  9.       mv "$F" "$F-$D"
  10.    fi
  11. done
Note: I put quotes around filenames to avoid problems with spaces in them.
Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Adding Date & time stamps to filename manish.s UNIX for Dummies Questions & Answers 1 03-13-2009 05:00 AM
Adding date and time to a log file ltodd2 UNIX for Dummies Questions & Answers 2 12-15-2008 01:37 PM
problem with displaying date and adding time FuncMx Shell Programming and Scripting 1 10-10-2008 01:06 AM
Processing a log file based on date/time input and the date/time on the log file primp Shell Programming and Scripting 4 03-16-2008 12:23 PM

All times are GMT -4. The time now is 05:21 PM.