Computing.Net > Forums > Unix > UNIX script: *.txt->*yymmddhhmm.txt

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

UNIX script: *.txt->*yymmddhhmm.txt

Reply to Message Icon

Name: Archie Ward Julien
Date: November 5, 2002 at 02:39:37 Pacific
OS: PC clone
CPU/Ram: AMD XP2000 256MB
Comment:

Out of nowhere, I suddenly need to rename a file with the current date and time automatically appended e.g. the script should prompt you for a filename, you type an input filename such as “fred.txt,” the function of the program is to change "fred.txt" to "fred0210311600.txt" if executed at 16:00 on 31/10/02. Also, what would be a nice book, brief if possible, describing Unix scripting?



Sponsored Link
Ads by Google

Response Number 1
Name: jimbo
Date: November 5, 2002 at 03:22:22 Pacific
Reply:

#! /bin/ksh

# change a file name to include current date
# File should be in the current directory
DATE=`date +%y%m%d%H%M`

if [[ $# == 0 ]] ; then
print "No file specified"
exit 1
fi

NAME=${1%.*}
EXT=${1##*.}

# Check if a file extension exists
if [[ "$NAME" == "$EXT" ]] ; then
mv $1 ${NAME}${DATE}
else
mv $1 ${NAME}${DATE}.$EXT
fi

-jim


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: UNIX script: *.txt->*yymmddhhmm.txt

unix scripts www.computing.net/answers/unix/unix-scripts/3942.html

Need kindly help about UNIX script! www.computing.net/answers/unix/need-kindly-help-about-unix-script/5562.html

basic unix scripting help www.computing.net/answers/unix/basic-unix-scripting-help/7686.html