If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Limit filenames to 31 chars

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-04-03, 02:52
jbveenstra jbveenstra is offline
Registered User
 
Join Date: Nov 2003
Posts: 2
Limit filenames to 31 chars

On our unix server we have a destination folder wich gets jpegs from the OPI server system.
These names are too long for reading on the Mac-clients. Limit is 31 characters.

How do I make a script wich watches the directory, recognizes the jpegs and limits the filenames to 31 characters while leaving the .jpg extension intact?

I usually use the c shell on both unix and Mac OS X

Regards,

JB

Last edited by jbveenstra; 11-04-03 at 02:56.
Reply With Quote
  #2 (permalink)  
Old 11-04-03, 04:59
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
Here's something to start you off. I don't use csh, sorry!

maxLength=31
while :; do
for fileName in *jpg; do
((${#fileName} > maxLength)) && {
newFileName=$(echo $fileName | sed "s/.*\(.\{$maxLength\}\)/\1/") &&
! test -f $newFileName &&
mv $fileName $newFileName ||
echo Unable to rename $fileName
}
done
sleep 60
done

Last edited by Damian Ibbotson; 11-04-03 at 05:02.
Reply With Quote
  #3 (permalink)  
Old 11-04-03, 08:14
jbveenstra jbveenstra is offline
Registered User
 
Join Date: Nov 2003
Posts: 2
Thank you very much. It works very good.

For testing purposes I've commented out the line

while :; do

otherwise I have to restart before the process stops.

I have also difficulties understanding some commands

((${#fileName} > maxLength))

What is this doing?

And I don't fully understand the sed command

sed "s/.*\(.\{$maxLength\}\)/\1/"


.*\(.\{$maxLength\}\) is the pattern? and \1 the procedure?
What does \1 do?

What I want to change is that the name is truncated before .jpg and that a duplicate gets a number added 00, 01, 02 and so on.
I lowered the variable maxlength to 29 to create space for this number.

You 've already guessed it by now, I'm a beginner in scripting. But I'm working on it.

Regards,

JB
Reply With Quote
  #4 (permalink)  
Old 11-04-03, 09:15
Damian Ibbotson Damian Ibbotson is offline
Padawan
 
Join Date: Jun 2002
Location: UK
Posts: 525
((${#fileName} > maxLength))

${#variable} is the character length of 'variable', so this command is testing if the length is greater than maxlength.

sed "s/.*\(.\{$maxLength\}\)/\1/"

The above command evaluates to...

sed 's/.*\(.\{31\}\)/\1'

What this is doing is matching 0 or more characters ('.' is a wildcard in sed), followed by 31 more characters. The .\{31\} pattern is contained in parentheses \(\) and so the matched pattern is held in a buffer by sed. As it is the first (and in this case, only) pattern in parentheses, the related buffer is buffer 1, referenced by \1. Simply put, it is replacing the whole string with the last 31 characters of that string.

What I want to change is that the name is truncated before .jpg and that a duplicate gets a number added 00, 01, 02 and so on.
I lowered the variable maxlength to 29 to create space for this number.

Hmmm... Something like this...

maxLength=31
typeset -Z2 fileCounter=0
while :; do
for fileName in *.jpg; do
((${#fileName} > maxLength)) && {
newFileName=$(echo $fileName | sed "s/.*\(.\{$maxLength\}\)/\1/") &&
while test -f $newFileName; do
newFileName=$fileCounter$(echo $fileName | sed "s/.*\(.\{$((maxLength - 2))\}\)/\1/")
fileCounter=$((fileCounter + 1))
done
mv $fileName $newFileName
}
fileCounter=0
done
sleep 60
done

Last edited by Damian Ibbotson; 11-04-03 at 09:45.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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