LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   script to rename files recursively (http://www.linuxquestions.org/questions/linux-general-1/script-to-rename-files-recursively-879461/)

rs232 05-08-2011 06:43 AM

script to rename files recursively
 
Hi all, I'm planning to writing a script to rename files recursively.

To be said that I'm using /bin/sh (not /bin/bash) as this is the only shell available on the busybox of the linux router (tomato) I'm using.

Basically I would like to rename files with extension .jpg using as a suffix the filename of another file in the very same directory with extension .avi

The reason for this is because pretty much all the DLNA devices like modern TV playing .avi files will display a thumbnail of the video when browsing the filesystem, however to do so they'll need .jpg image wit hthe same filename of the video in the very same directory.

e.g.

BEFORE (no thumbnail shown) -------------------------------

DIR1
video1.avi
34q623476347.jpg

DIR2
video2.avi
dathlstooerigowis.jpg

DIR3
video3.avi
erahyrtjtykrtbhmwrpogjwp.jpg

AFTER (thumbnail working fine) --------------------------------

DIR1
video1.avi
video1.jpg

DIR2
video2.avi
video2.jpg

DIR3
video2.avi
video2.jpg

------------------------------------------

Have you seen around anything like this before?
just asking before writing any script at all...

Thanks!

carltm 05-08-2011 08:10 AM

I don't know of any command that can do this, however it should be
pretty easy to write a script. I haven't tested this, so take a
look at it before running it.

Code:

#!/bin/sh

# This script assumes there is exactly one jpg file in any
# directory that has an avi file.  It renames the jpg file
# to match the avi file.

VideoDirectory="/path/to/videos"

find "$VideoDirectory" -name '*.avi' |\
while read Video; do
  OldName="`echo $Video | sed 's|/[^/]*$|/*.jpg|'`"
  NewName="`echo $Video | sed 's/avi$/jpg/'`"
  if [ ! -f "$NewName" ] && [ "$NewName != "$OldName" ]; then
    mv "$OldName" "$NewName"
  fi
done


rs232 05-08-2011 12:47 PM

That's a good starting point... I have to be honest I'm a bash guy myself and find difficult to work with sh...

Any ways, I changed:

if [ ! -f "$NewName" ] && [ "$NewName != "$OldName" ]; then

into:

if [ ! -f "$NewName" ] && [ "$NewName" != "$OldName" ]; then

as the script was complaining.

The other thing is: "find" in busybox doesn't like filenames with square brackets e.g. myvideo[2008].avi

If for a testI change a folder (and files) with something with no square brackets

root@busybox:/cifs2/temp/21_test# /cifs1/dlnarename.sh
mv: can't rename '/cifs2/temp/21_test/*.jpg': No such file or directory

root@busybox:/cifs2/temp/21_test# ls -la
myvideo_2008.avi
randomfile.jpg

catkin 05-08-2011 02:05 PM

Quote:

Originally Posted by rs232 (Post 4350139)
if [ ! -f "$NewName" ] && [ "$NewName" != "$OldName" ]; then

as the script was complaining.

AFAIK there's nothing wrong with doing it that way but it is unconventional. As long as busybox' bash is the same as others (some things are different under busybox) the, preferring [[ .. ]] over [ .. ] for reasons explained here, you could try if [[ ! -f "$NewName" && "$NewName" != "$OldName" ]]; then

What was the "complaining" message?

How big are the files? I vaguely recall a hard-to-solve busybox problem that was eventually solved when we realised that some utilities did not work with files greater than some size (2 GB?) and another version of ls (or find ... ?) had to be used -- it was part of the busybox installation but not the first found when searching the $PATH directories ... ?


All times are GMT -5. The time now is 11:23 PM.