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
 
LinkBack Search this Thread
Old 09-04-2009, 11:56 AM   #1
limdel
Member
 
Registered: May 2009
Location: Ho Chi Minh City, Vietnam
Distribution: CentOS 6.4, FEDORA 18 x64, Linux Mint 15 x64
Posts: 57

Rep: Reputation: 15
how to rename a file based on it creation date


Hi,

I need to rename a bunch of files based on their creation date.
I'd already search on google but without success.
Any idea are welcome.

thanks in advance
 
Old 09-04-2009, 12:17 PM   #2
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,029

Rep: Reputation: 368Reputation: 368Reputation: 368Reputation: 368
Code:
for file in *; do NEW_FILENAME=$(stat "$file" --format %y); echo mv "$file" "$NEW_FILENAME"; done
In bash.

You could do some additional parsing to $NEW_FILENAME if the original format doesn't suit you. When you are happy about the new file names, just remove "echo" and the real work will be done. Be sure to check carefully or do backups.
 
Old 09-04-2009, 12:27 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
It should be pointed out that *nix filesystems don't keep track of the creation date of files. You can only get time of last modification, which is what the "%y" option of stat gives you in the string above.
 
Old 09-04-2009, 12:35 PM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,029

Rep: Reputation: 368Reputation: 368Reputation: 368Reputation: 368
Correct. I forgot to mention that. %y gives you the date of the last modification.
 
Old 09-04-2009, 12:53 PM   #5
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware, Mint
Posts: 1,240

Rep: Reputation: 187Reputation: 187
Quote:
Originally Posted by limdel View Post
I need to rename a bunch of files based on their creation date.
What's the type of these files? I use nice scripts to rename JPG files taken from the camera using EXIF data to determine the dates of the creation of the images.
 
Old 09-04-2009, 09:31 PM   #6
limdel
Member
 
Registered: May 2009
Location: Ho Chi Minh City, Vietnam
Distribution: CentOS 6.4, FEDORA 18 x64, Linux Mint 15 x64
Posts: 57

Original Poster
Rep: Reputation: 15
@i92guboj & David the H: thanks your answer

@w1k0
the files are .mts files from my camcorder Canon HF10 (video:H264 audio:AC3).
The file are stored on the memory card and are named based on a auto-increment index:
00001.mts
00002.mts
etc

i'm creating a script that will:
-copy the .mts files to my HDD & rename it base on it's....last modified date (should be just fine since i do not edit the file on the device)
-create an .avi "easy playable copy" of the file using ffmpeg (great tool by the way)

Not a big deal, but it will be my first one.
 
Old 09-07-2009, 03:12 AM   #7
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,029

Rep: Reputation: 368Reputation: 368Reputation: 368Reputation: 368
Quote:
Originally Posted by limdel View Post
@i92guboj & David the H: thanks your answer

@w1k0
the files are .mts files from my camcorder Canon HF10 (video:H264 audio:AC3).
The file are stored on the memory card and are named based on a auto-increment index:
00001.mts
00002.mts
etc

i'm creating a script that will:
-copy the .mts files to my HDD & rename it base on it's....last modified date (should be just fine since i do not edit the file on the device)
The above snippet should work. Assuming that you are on the directory that contains the mts files. As said above, if you like the output just remove "echo" to do the real work.

Code:
for file in *
do
  NEW_FILENAME=$(stat "$file" --format %y)
  echo mv "$file" "/whatever/new/location/$NEW_FILENAME.mts"
done
I have no idea how to use ffmpeg to create an avi slideshow. I know it can do it though. Google should be able to answer that, or someone else around.
 
Old 09-22-2009, 03:12 AM   #8
limdel
Member
 
Registered: May 2009
Location: Ho Chi Minh City, Vietnam
Distribution: CentOS 6.4, FEDORA 18 x64, Linux Mint 15 x64
Posts: 57

Original Poster
Rep: Reputation: 15
Hi,
Just come back to post my final first script.
ANY comments, improvements, optimizations, corrections are WELCOME.
Thanks to all for your help

Code:
#!/bin/bash
#--DESCRIPTION:
#--	-locate MTS files from the camcorder storage
#--	-make a creation date tagged copy on HDD
#--	-create a "thumbnail" video for easy review and share
#--
#--TO DO BEFORE RUN
#--	-Define path to backup directory on the hdd
MTSBCKROOT=/home/`whoami`/Videos/camcorder;
#-- or MTSBCKROOT=~/Videos/camcorder;
#-- BUT NOT MTSBCKROOT="~/Videos/camcorder"; WON'T WORK, i lost a few hours founding why !!!  GRRRrrrrr  !!!!
#--
#--Start Script
echo "Start `basename $0`"
echo "List of mounted media found:"
ls -1 /media
echo -e "Enter camcorder media to backup: \c"
read CAMCORDERSTOR
#--Recursive search of .mts file on the media and start loop on each file
find /media/$CAMCORDERSTOR -iname "*.mts" -print0 | while read -d $'\0' MTSFILE;
do
#--Just in case destination folder does not exist
mkdir -p $MTSBCKROOT/mts
#--Build new name as <Last_Modif_Date as 'YYYY-MM-DD_HH:NN:SS">_<Filename_With_Ext>
NEWFILENAME=$(stat $MTSFILE --format %y|awk '{print $1"_"$2}'|cut -f1 -d'.')"_"`basename $MTSFILE`;
#--Make a backup copy of original mts file on HDD
cp -Rvp $MTSFILE $MTSBCKROOT/mts/$NEWFILENAME;
#--Create an AVI copy from the MTS file
ffmpeg -i $MTSBCKROOT/mts/$NEWFILENAME -f avi -vcodec libxvid -b 9000k -deinterlace -s 960x540 -acodec libmp3lame $MTSBCKROOT/$NEWFILENAME.avi;
done;
echo "End of `basename $0`"
One thing that i was looking for, was to get a menu-like choice for the mounted media listing (filled by "ls -1 /media") , same as choice in DOS.
soemthing like:
Code:
Choose your source media:
1-/media/<usbflahsdrive>
2-/media/<camcorderinternalmem>
3-/media/<camcorderSDcard>
Enter your choice:
Is it possible ?


For ffmpeg installation, and on my system (Linux 2.6.30.5-43.fc11.i686.PAE #1 SMP 2009 i686 i686 i386 GNU/Linux), i did:

install rpmfusion free-only repository
Code:
su -c 'rpm -Uvh http://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-stable.noarch.rpm
then Install ffmpeg
Code:
yum -y install ffmpeg
and
Code:
yum install gstreamer-ffmpeg.i586

Last edited by limdel; 09-22-2009 at 03:33 AM. Reason: add "yum install gstreamer-ffmpeg.i586"
 
Old 09-22-2009, 06:24 AM   #9
limdel
Member
 
Registered: May 2009
Location: Ho Chi Minh City, Vietnam
Distribution: CentOS 6.4, FEDORA 18 x64, Linux Mint 15 x64
Posts: 57

Original Poster
Rep: Reputation: 15
Sorry, but there is a huge bug on my script...
it do not loop !
It perform the 2 task cp and ffmpeg for the first file listed by the find, then exit !

What's wrong ?
 
Old 09-22-2009, 06:25 PM   #10
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
You can debug using

Code:
#!/bin/bash
set -xv
as the first 2 lines of your script. It'll show you exactly what its doing.
Just FYI, you can have blank lines in a script, and I would, that's a bit dense for reading and will get harder for longer scripts.
 
Old 09-22-2009, 09:54 PM   #11
limdel
Member
 
Registered: May 2009
Location: Ho Chi Minh City, Vietnam
Distribution: CentOS 6.4, FEDORA 18 x64, Linux Mint 15 x64
Posts: 57

Original Poster
Rep: Reputation: 15
It's work !

Thanks Chris for your post, it help to isolate the problem and found out the solution; i split the original script in 2 files and and use option "-exec" with the "find" command.

The main that search for .MTS file: bckcamcorder.sh:
Code:
#!/bin/bash
#
#--DESCRIPTION:
#--	-locate MTS files from the camcorder storage
#--	-on each mts files, launch external script that
#--		1-make a creation date tagged copy on HDD
#--		2-create a "thumbnail" video for easy review and share
#--
#--TO DO BEFORE RUN:
#--	-Define path to backup directory on the hdd
MTSBCKROOT=/home/`whoami`/Videos/camcorder;
#-- or MTSBCKROOT=~/Videos/camcorder;
#-- BUT NOT MTSBCKROOT="~/Videos/camcorder"; WON'T WORK, i lost a few hours founding why !!!  GRRRrrrrr  !!!!
#--

#--Start Script
echo "Start `basename $0`"

#--Display mounted media
echo "List of mounted media found:"
ls -1 /media

#--Request input media to scan
echo -e "Enter camcorder media to backup: \c"
read CAMCORDERSTOR

#--Just in case backup destination folder does not exist, create it
mkdir -p $MTSBCKROOT/mts

#--Recursive search of .mts files on the media and loop on each files to run external script
find /media/$CAMCORDERSTOR -iname "*.mts" -exec ~/myscript/mts2avi.sh $MTSBCKROOT {} \;
done

echo "End of `basename $0`"
and mts2avi.sh that perform a backup copy and create the video thumbnail:
Code:
#!/bin/bash
#--Build new name as <Last_Modif_Date as 'YYYY-MM-DD_HH:NN:SS">_<Filename_With_Ext>
NEWFILENAME=$(stat $2 --format %y|awk '{print $1"_"$2}'|cut -f1 -d'.')"_"`basename $2`

#--Make a backup copy of original mts file on HDD
cp -Rvp $2 $1/mts/$NEWFILENAME

#--Create an AVI copy from the MTS file ("-acodec libfaac -ar 22050" or "-acodec libmp3lame")
ffmpeg -i $1/mts/$NEWFILENAME -f avi -vcodec libxvid -b 9000k -deinterlace -s 960x540 -acodec libfaac -ar 22050 $1/$NEWFILENAME.avi
AND IT'S WORK for all mts files found on the media !

Thanks all for your help !

Last edited by limdel; 09-23-2009 at 12:53 AM.
 
Old 09-23-2009, 06:41 AM   #12
limdel
Member
 
Registered: May 2009
Location: Ho Chi Minh City, Vietnam
Distribution: CentOS 6.4, FEDORA 18 x64, Linux Mint 15 x64
Posts: 57

Original Poster
Rep: Reputation: 15
and finally, the cherry on the cake...

and here the Media list choice

replace code:
Code:
#--Display mounted media
echo "List of mounted media found:"
ls -1 /media

#--Request input media to scan
echo -e "Enter camcorder media to backup: \c"
read CAMCORDERSTOR
by:
Code:
#--Build List menu with mounted media storage
PS3="Type media ID then press enter:"
echo "Choose the camcorder storage from the list below."
select CAMCORDERSTOR in `ls /media`; do
	break
done
and i can have an easy media selector like this:
Code:
Choose the camcorder storage from the list below.
1) DSL
2) USBFD12GB
3) CAMCORDERMEM
4) CAMCORDERSD
Type media ID then press enter:
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File creation date Jay5487 Linux - Newbie 2 03-25-2008 07:47 AM
file creation date prashantbhushan Linux - General 2 05-29-2007 08:32 AM
Rename file based on its own date/time stamp airman99 Linux - General 19 09-05-2006 07:52 AM
Rename file with date stamp MacSob Linux - General 6 09-13-2005 01:30 PM
ls -al file creation date blackzone Linux - Newbie 2 08-23-2004 01:31 AM


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

Main Menu
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