[SOLVED] how to rename a file based on it creation date
Linux - NewbieThis 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
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.
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.
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.
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.
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309
Rep:
Quote:
Originally Posted by limdel
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.
@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)
@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.
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"
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.
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 !
#--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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.