Shell scripts: determining filename length

Mike Klinke mklinke at axsi.com
Wed Sep 7 15:40:08 UTC 2005


On Wednesday 07 September 2005 10:06, Brian D. McGrew wrote:
> Good morning.
>
> I'm working on some shell scripts that process directories full
> of times shared by Samba and used by Windows people.  We all know
> that Windows people have a habit of really, really, really, long
> filenames.
>
> In a shell script, inside a `for f in *` loop, how can I go about
> finding how long a filename is ... And then truncate the name,
> keeping the three letter extension, to less than 64 characters?
>

Take a look at 'man bash', specifically the section on 'parameter 
expansion'.  You'll see a few entries associated with length that 
might be useful.  For example: 

======== strlength.sh ========
#!/bin/bash

echo "The string has "${#1}" characters"
echo "First 10 characters: "${1:0:10}
echo "Last 3 characters: "${1:${#1}-3:3}
===============================

$ ./strlength.sh 123456789012345.txt

The string has 19 characters
First 10 characters: 1234567890
Last 3 characters: txt


Regards, Mike Klinke




More information about the redhat-list mailing list