LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 05-08-2011, 06:43 AM   #1
rs232
Member
 
Registered: Oct 2005
Posts: 51

Rep: Reputation: 0
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!
 
Old 05-08-2011, 08:10 AM   #2
carltm
Member
 
Registered: Jan 2007
Location: Canton, MI
Distribution: CentOS, SuSE, Red Hat, Debian, etc.
Posts: 703

Rep: Reputation: 99
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
 
Old 05-08-2011, 12:47 PM   #3
rs232
Member
 
Registered: Oct 2005
Posts: 51

Original Poster
Rep: Reputation: 0
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

Last edited by rs232; 05-08-2011 at 12:49 PM.
 
Old 05-08-2011, 02:05 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by rs232 View Post
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 ... ?
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Rename recursively unihiekka Linux - Newbie 10 05-26-2010 01:55 AM
[SOLVED] how to rename files recursively, only keeping last x digits plus extension furryspider Programming 2 11-29-2009 12:55 PM
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 08:00 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM
How to recursively rename files using their directory name pattern ceg4048 Linux - General 2 09-28-2005 01:16 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 03:46 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration