Operating System - HP-UX
1823956 Members
3763 Online
109667 Solutions
New Discussion юеВ

Script Needed to Count Characters in a filename

 
SOLVED
Go to solution
S-un-B-ix-S
Advisor

Script Needed to Count Characters in a filename

I am in the process of migrating some data from one system to another.
The new system has a character limit of 128 for a filename, and I know some files on the old system exceed this.

Does anyone have a script that could loop through my system and generate a log file of all file names that are longer than 128 characters?

TIA...
16 REPLIES 16
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Script Needed to Count Characters in a filename

I'll assume that you mean only the last part of any pathname:

cd to desired starting directory
find . -exec basename {} \; | awk '{if (length($0) > 128) {print $0}}'
If it ain't broke, I can fix that.
Sandman!
Honored Contributor

Re: Script Needed to Count Characters in a filename

What are your source and target systems...I mean what is their OS type?
S-un-B-ix-S
Advisor

Re: Script Needed to Count Characters in a filename

Mac 10.2 w/ local storage

to

HPUX 11.23 w/(UTF) remote storage
Peter Nikitka
Honored Contributor

Re: Script Needed to Count Characters in a filename

Hi,

it's not clear if you mean, that the full pathname may not exceed this limit or just a plain filename.
Plain filename <128 chars will be reported by
find . | awk -F/ 'length($NF) >=128'

A directory having a name >= 128 will be reported only once - so you can use this list as an input for a renaming script.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
S-un-B-ix-S
Advisor

Re: Script Needed to Count Characters in a filename

Sorry, I guess I need t be a bit more clear.

Pathname = /home/bob1/docs/file1
Filename = file1

It doesnt matter if the pathname all together exceeds 128 characters, but the filename must not exceed 128 characters.

I also need to account for white spaces in the filenames, and to be able to count the whitespace as a character.

File example: Job Advertising Listing.qxd
Sandman!
Honored Contributor

Re: Script Needed to Count Characters in a filename

Just curious since HPUX 11.23 supports file names upto 256 charcters then where are you hitting the 128 character limit?
S-un-B-ix-S
Advisor

Re: Script Needed to Count Characters in a filename

This data will be eventually moved off the HPUX server onto a NAS device that only supports 128 characters, and part of the data migration process is that we take care of all illegal character violations now.
A. Clay Stephenson
Acclaimed Contributor

Re: Script Needed to Count Characters in a filename

The script I supplied earlier does all of that.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor

Re: Script Needed to Count Characters in a filename

If you are concerned about whitespaces in filenames then a small change is needed:

find . -exec basename {} \; | awk '{if (length($0) > 128) {print $0}}'

becomes:

find . -exec basename "{}" \; | awk '{if (length($0) > 128) {print $0}}'
If it ain't broke, I can fix that.
S-un-B-ix-S
Advisor

Re: Script Needed to Count Characters in a filename

I am seeing this when I run your script:

.:basename./Thankyou/Thankyoucards report:No such file or directory

bash-2.05a# find . -name 'Thankyoucards report'
./Thankyou/Thankyoucards report

I am not really familiar with the basename function....do it have a problem with relative paths perhaps?
A. Clay Stephenson
Acclaimed Contributor

Re: Script Needed to Count Characters in a filename

basename./Thankyou/Thankyoucards report:No such file or directory

WRONG! WRONG! WRONG! (3 wrongs don't make it right.)

Wrong No. 1: Note no whitespace between basename and ./Thankyou... so the shell is looking for
basename./Thankyou/Thankyoucards and that command can't be found.
Wrong No. 2: No quotes so "report" is treated as a suffix argument to basename
Wrong No. 3: Basename is a command not a function. Man basename for details.

The final example where I placed {} inside double quotes should do just what you want.

If it ain't broke, I can fix that.
Sandman!
Honored Contributor

Re: Script Needed to Count Characters in a filename

Clay's script runs fine on my machine. Perhaps it has got to do something with Mac 10.2.
S-un-B-ix-S
Advisor

Re: Script Needed to Count Characters in a filename

thank you very much for all of your help.
Peter Nikitka
Honored Contributor

Re: Script Needed to Count Characters in a filename

Hi,

my one-liner meets all your requests as well, IMHO.

mfG Peter

The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Bill Hassell
Honored Contributor

Re: Script Needed to Count Characters in a filename

A more efficient way to count the length of a string is to let the shell do it (basename is an external command).

FULLPATH="long/path/name/with/special File name"
NAMEONLY="${FULLPATH##*/}"
NAMELENGTH=${#NAMEONLY}

Notice the use of ".." marks to include any embedded spaces. If you need to check whether a filename has imbedded spaces or other special characters, use the class matching available in tr. You can remove all the 'normal' characters and if anything is left, the filename contains sp[ecial characters:

[ "$(echo "$NAMEONLY" | tr -d '[:alnum:]')" != "" ] && echo "filename has non-alphanumeric chars"

By using shell built-ins, the overhead in calling external routines is eliminated. Here are some shell constructs that replace external commands:

basename MYPATH
${MYPATH##*/}

dirname MYPATH
${MYPATH%*/}

To reduce fully qualified domain names such as cpu1.mysite.com to just cpu1:

MYCPU=cpu1.mysite.com
${MYCPU%%.*}

To parse out the 4 numbers in a network address:

MYADDR=1.23.45.67
echo $MYADDR | IFS=. read ADDR1 ADDR2 ADDR3 ADDR4
echo $ADDR1 $ADDR2 $ADDR3 $ADDR4


Bill Hassell, sysadmin
S-un-B-ix-S
Advisor

Re: Script Needed to Count Characters in a filename

everyone's responses helped greatly.