LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 09-01-2009, 07:02 PM   #1
themonkman
LQ Newbie
 
Registered: Dec 2005
Posts: 2

Rep: Reputation: 0
Bash Script - adding file modification date to end of filenames in directory


Hello all,

I'm not adept at bash scripting, but I need to find a way to take a group of files in a directory and add their file modification dates to the end of the filename. For example I have:

LogDir/

-rw-r----- 1 test users 0 Aug 1 21:50 test.log.1
-rw-r----- 1 test users 0 Aug 3 21:50 test.log.2
-rw-r----- 1 test users 0 Aug 5 21:50 test.log.3
-rw-r----- 1 test users 0 Aug 7 21:50 test.log.4
-rw-r----- 1 test users 0 Aug 8 21:50 test.log.5

And I need the script to convert the all the files in the directory to:

LogDir/

-rw-r----- 1 test users 0 Aug 1 21:50 test.log.20090801
-rw-r----- 1 test users 0 Aug 3 21:50 test.log.20090803
-rw-r----- 1 test users 0 Aug 5 21:50 test.log.20090805
-rw-r----- 1 test users 0 Aug 7 21:50 test.log.20090807
-rw-r----- 1 test users 0 Aug 9 21:50 test.log.20090809

Any help would be greatly appreciated. I'm just starting to learn bash scripting but I still have a long way to go.
 
Old 09-01-2009, 07:40 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,765
Blog Entries: 54

Rep: Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976Reputation: 2976
Code:
# find in directory "LogDir" all items of type "file" with name glob "test.log.*",
find LogDir -type f -iname test.log.\* | while read FILE; do
 # Use 'stat' to extract the modification time,
 mtime=($(stat -c "%y" "${FILE}"))
 # Prepare the new filename chopping off numbering,
 NFILE="${FILE//g.*/g.}"
 # Propose renaming
 echo mv "${FILE}" "${NFILE}.${mtime[0]//-/}"
done
Run, and if you think this'll work change "echo mv" to read just "mv".

Code:
function help() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html 
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html 
http://www.tldp.org/LDP/abs/html/"; }
 
Old 09-02-2009, 12:45 AM   #3
themonkman
LQ Newbie
 
Registered: Dec 2005
Posts: 2

Original Poster
Rep: Reputation: 0
Thank you so much! That worked perfectly.
 
  


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
Get file modification date/time in Bash script cmfarley19 Programming 13 12-21-2014 12:05 PM
how to increment the date in a file name via bash script tensigh Programming 10 07-23-2010 04:49 AM
Bash script -----------syntax error: unexpected end of file ArthurHuang Programming 2 05-01-2009 11:29 AM
Bash script - syntax error: unexpected end of file Mr Pink Programming 7 12-19-2008 07:31 AM
File Modification Date Comparison MadTurki Programming 7 03-08-2004 05:02 PM


All times are GMT -5. The time now is 04:36 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration