LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to rename a file based on it creation date (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-rename-a-file-based-on-it-creation-date-752694/)

limdel 09-04-2009 11:56 AM

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

i92guboj 09-04-2009 12:17 PM

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.

David the H. 09-04-2009 12:27 PM

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.

i92guboj 09-04-2009 12:35 PM

Correct. I forgot to mention that. %y gives you the date of the last modification.

w1k0 09-04-2009 12:53 PM

Quote:

Originally Posted by limdel (Post 3669868)
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.

limdel 09-04-2009 09:31 PM

@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.

i92guboj 09-07-2009 03:12 AM

Quote:

Originally Posted by limdel (Post 3670443)
@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.

limdel 09-22-2009 03:12 AM

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

limdel 09-22-2009 06:24 AM

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 ?

chrism01 09-22-2009 06:25 PM

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.

limdel 09-22-2009 09:54 PM

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 !

limdel 09-23-2009 06:41 AM

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:



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