Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
04-26-2009, 01:15 PM
|
#1
|
LQ Newbie
Registered: Aug 2004
Posts: 26
Rep:
|
Batch/bulk rename with ascending dates?
I have molested google with this question for a few hours and came up with naught.
I have 500+ *.gif files that are ordered 1.gif 2.gif etc... I have tried the bulk rename utility in thunar and it *almost* works. I can rename the files by creation date and get them into a yyyy-mm-dd format. Problem is over a hundred files were created on the same dates. I need them all with different dates *does not matter what dates* in order to import them into a DB.
Is there some tool/script that can be given a start date and and rename the files in a ascending order?
Thanks in advance for any help with this.
Venn
|
|
|
04-27-2009, 02:34 AM
|
#2
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,441
|
In Perl:
Code:
#!/usr/bin/perl -w
use locale; # Ensure correct charset for eg 'uc()'
use Date::Calc qw(Add_Delta_Days);
use DirHandle; # get sorted list of files to process
use strict; # Enforce declarations
my ( $today, $curr_yr, $curr_mth, $curr_day,
$next_yr, $next_mth, $next_day,
@sorted_list, $file
);
# Date calc
$today= get_todays_date();
($curr_yr, $curr_mth, $curr_day) = (substr($today, 0, 4),
substr($today, 4, 2),
substr($today, 6, 2));
@sorted_list = get_file_list(".");
for $file (@sorted_list)
{
($next_yr, $next_mth, $next_day) = Add_Delta_Days($curr_yr, $curr_mth,
$curr_day, +1);
$next_mth = (length($next_mth) < 2 ) ? "0".$next_mth : $next_mth;
$next_day = (length($next_day) < 2 ) ? "0".$next_day : $next_day;
rename($file, "${next_yr}-${next_mth}-${next_day}.gif");
($curr_yr, $curr_mth, $curr_day) = ($next_yr, $next_mth, $next_day);
}
#*****************************************************************************
#
# Function : get_file_list
#
# Description : Get list of gifs filenames in numeric order
#
# Params : none
#
# Returns : array of sorted filenames
#
#******************************************************************************
sub get_file_list
{
my $dir = shift;
my $dirh = DirHandle->new($dir) or die "can't opendir $dir: $!";
# Grab the files (gif only)
return sort grep { /\.gif$/ } # choose only gif files
$dirh->read(); # read all entries
}
#******************************************************************************
#
# Function : get_todays_date
#
# Description : Get current yr, mth, day-of-mth
#
# Params : none
#
# Returns : $today YYYYMMDD
#
#******************************************************************************
sub get_todays_date
{
my (
$year, # year ( -1900 )
$mth, # month ( -1 )
$mday, # day-of-mth
$today # today string: yyyymmdd
);
# Get today
# NB: Mth array starts at zero
($mday, $mth, $year) = (localtime)[3..5];
$today = sprintf( "%04d%02d%02d", $year+1900, $mth+1, $mday );
return $today;
}
a bit quick and dirty, but works for me
input
Code:
1.gif 2.gif 3.gif 4.gif 5.gif
output
Code:
2009-04-28.gif 2009-04-30.gif 2009-05-02.gif
2009-04-29.gif 2009-05-01.gif
note that in this form you put the perl prog in the dir containing the gifs
|
|
|
04-27-2009, 03:48 AM
|
#3
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
shell and GNU date
Code:
i=0
ls |sort -n | while read FILE
do
d=$(date +"%Y-%m-%d" -d "$i day")
echo mv "$FILE" ${d}.gif
i=$(( i+1 ))
done
|
|
|
04-27-2009, 06:32 PM
|
#4
|
LQ Newbie
Registered: Aug 2004
Posts: 26
Original Poster
Rep:
|
WOW
Thank you ghostdog74 and chrism01! Both solutions worked without flaw. Turned out I needed to start the image date back in 2007 but worked around that problem with Thunar bulk search and replace.
Thanks again.
Venn
|
|
|
All times are GMT -5. The time now is 02:18 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|