LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 02-04-2015, 06:46 PM   #1
linux2man
Member
 
Registered: Oct 2006
Posts: 34

Rep: Reputation: 16
Script Rename files


Dears,
I want to create script to rename multiple file names from unix date to date for example
filename_1421907815_department.txt
rename to
filename_2015_01_22_08_23_department.txt
 
Old 02-04-2015, 07:14 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 17,916

Rep: Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689Reputation: 3689
Quote:
Originally Posted by linux2man View Post
Dears,
I want to create script to rename multiple file names from unix date to date for example
filename_1421907815_department.txt
rename to
filename_2015_01_22_08_23_department.txt
OK...so post what you've written/done so far. You've asked several questions like this, without showing effort of your own. We will be happy to help, but we're not going to write it for you.
 
Old 02-04-2015, 07:51 PM   #3
Ser Olmy
Senior Member
 
Registered: Jan 2012
Distribution: Slackware
Posts: 2,403

Rep: Reputation: Disabled
You can convert a Unix epoch to a different format using the date command. To get the format you specified above (Year_Month_Day_Hour_Minute), this should do the trick:
Code:
date -d @1421907815 +%Y_%m_%d_%H_%M
If every filename has the exact format "[sometext]_[epoch]_[sometext].txt" and there are no other underscores anywhere (important!), the following sed commands will filter out each part of the file names:
  • sed 's/_.*//' - returns everything up to the first underscore
  • sed 's/.*_//g' - returns only the part after the last underscore
  • sed -e 's/[^_]*_//' -e 's/_.*//' - returns the part between the first and the second underscore
Combining these, you can convert the filenames and perform the rename operation with this one-liner:
Code:
find *txt | while read x ; do mv "$x" "$(echo $x | sed 's/_.*//')_$(date -d @$(echo $x | sed -e 's/[^_]*_//' -e 's/_.*//g') +%Y_%m_%d_%H_%M)_$(echo $x | sed 's/.*_//g')" ; done
Now, I'm absolutely certain there's a more elegant way to do this, and in particular I have no doubt that a more readable version of the above could be created with awk or perl or perhaps by formatting output with printf, but the snippet above took 5 minutes to create and it does get the job done.

(Oh, and don't try to run this recursively, as the regular expressions don't take path names into account.)

Last edited by Ser Olmy; 02-04-2015 at 07:52 PM.
 
Old 02-10-2015, 07:38 PM   #4
overlook
LQ Newbie
 
Registered: May 2006
Location: Sweden
Distribution: Custom
Posts: 12

Rep: Reputation: 1
A sed-less alternative (takes filenames as arguments):
Code:
#!/bin/bash
for f in ${@} ; do
  x="$(echo ${f} | cut -f2 -d_)"
  y="$(date -d @${x} +%Y_%m_%d_%H_%M)"
  mv -v "${f}" "${f//${x}/${y}}"
done
 
  


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
Rename files using script vadslogin Linux - Newbie 6 01-31-2012 06:46 AM
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 09:00 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-23-2008 12:25 AM
Script to rename files nazs Programming 15 03-31-2007 03:12 PM
Script to Rename Many Files geeman2.0 Programming 3 04-05-2006 02:45 PM


All times are GMT -5. The time now is 10:14 AM.

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