LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-05-2011, 05:23 AM   #1
nandi
LQ Newbie
 
Registered: Oct 2011
Posts: 7

Rep: Reputation: Disabled
"How to rename files with current date stamp appended into name using shell script?"


Hi! As its my first day into shell scripting so I don't know much about it.
I need to learn that how a script will rename files in a directory to a new name appended with current date stamp into name.

Like file before : somename.txt
after: somename_cureent date_time.txt

How I can do it??

Thanks,
 
Old 10-05-2011, 05:50 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
I would say you will need a loop of some kind (possibly for), mv to rename them and a man on date command will help with getting the date on the end.
I would also suggest looking up process substitution.
 
Old 10-05-2011, 06:49 AM   #3
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,232

Rep: Reputation: 299Reputation: 299Reputation: 299
I tend to use something like:

Code:
DATE=$(date +%Y%m%d)
So you could then add
Code:
mv "$FILENAME" "$FILENAME"_$DATE.txt
But that doesn't include the time. You can get the time formats from the date man page.
 
Old 10-05-2011, 10:14 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
Hey, no fair just giving him the answer!


If you haven't seen this yet, I recommend reading through this guide. It will give you a good grounding in all the basics of bash scripting.

http://mywiki.wooledge.org/BashGuide


And some other useful bash scripting references:

http://www.linuxcommand.org/index.php
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
http://www.gnu.org/software/bash/manual/bashref.html
http://wiki.bash-hackers.org/start
 
Old 10-05-2011, 11:36 AM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,051

Rep: Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852Reputation: 852
Quote:
Originally Posted by nandi View Post
Hi! As its my first day into shell scripting so I don't know much about it.
I need to learn that how a script will rename files in a directory to a new name appended with current date stamp into name.

Like file before : somename.txt
after: somename_cureent date_time.txt

How I can do it??

Thanks,
does this work for you:
http://www.linuxquestions.org/questi...0/#post3566935
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to write shell script that adds an extension ".new" to all the files in a direct ayongying Programming 2 05-28-2010 11:09 AM
samba - windows files lose "created on" date stamp when moved to samba share on linux jaredk51 Linux - Software 5 02-19-2010 04:13 PM
shell script to recursively "compare" all files in a directory... silex_88 Programming 3 05-12-2007 05:24 AM
Rename file with date stamp MacSob Linux - General 6 09-13-2005 02:30 PM
Monthly Archiving Script... help with "date" & "cron" Supp0rtLinux Linux - Software 3 01-03-2003 10:29 PM


All times are GMT -5. The time now is 12:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration