General
cancel
Showing results for 
Search instead for 
Did you mean: 

check valid filename length of all .abc files in current⊂ dir

 
hak
Occasional Visitor

check valid filename length of all .abc files in current⊂ dir

hi,
how to check valid filename length of all .abc files in current⊂ dir.
lets take i want to check that the filename of all the .abc files should be less than 50 chars

thakx in advance
11 REPLIES 11
curt larson_1
Honored Contributor

Re: check valid filename length of all .abc files in current⊂ dir

tdir=your top directory
l=50 # adjust to weather you want to include the .abc extentsion or not

find $dir -type f -name "*.abc" |
while read fileName
if (( ${#fileName > 50 )) ;then
print "$fileName is greater then $l characters"
fi
done
Bill Hassell
Honored Contributor

Re: check valid filename length of all .abc files in current⊂ dir

There are a typos of typos in the example: tdir vs. $dir; l=50 but 50 hardcoded in the test; missing \ at end of find line; missing } in ${#fileName. This will work better:

DIR=/tmp
MAXLEN=50 # max length for filename

find $DIR -type f -name "*.abc" | \
while read FILENAME
do
if (( ${#FILENAME} > $MAXLEN ))
then
print "$FILENAME is greater then $MAXLEN characters"
fi
done


Bill Hassell, sysadmin
curt larson_1
Honored Contributor

Re: check valid filename length of all .abc files in current⊂ dir

thanks for the assistance bill
i should get my other eye open before answering more of these.
Sridhar Bhaskarla
Honored Contributor

Re: check valid filename length of all .abc files in current⊂ dir

Hi,

Only a little logical correction in the script above. 'find /directory' will print the filenames with full path starting from /directory. For ex., /directory/file1.abc /directory/subdir/file2.abc etc.,. So, merely checking the entry without skipping the directory path will not give correct results.

So, you will need to modify the above script as

DIR=/tmp
MAXLEN=10 # max length for filename

find $DIR -type f -name "*.depot" | \
while read FILENAME
do
BASE=$(basename $FILENAME)
if (( ${#BASE} > $MAXLEN ))
then
print "$FILENAME is greater then $MAXLEN characters"
fi
done
DIR=/tmp
MAXLEN=10 # max length for filename

find $DIR -type f -name "*.abc" | \
while read FILENAME
do
BASE=$(basename $FILENAME)
if (( ${#BASE} > $MAXLEN ))
then
print "$FILENAME is greater then $MAXLEN characters"
fi
done

'basename $FILENAME' is important. Also if you do not want .abc to be counted in the filename check, then make MAXLEN to "54" as four characters will be .abc.

-Sri

You may be disappointed if you fail, but you are doomed if you don't try
Sridhar Bhaskarla
Honored Contributor

Re: check valid filename length of all .abc files in current⊂ dir

My Pasting operation was so horrible, I pasted my test script also.. So, ignore the previous post and look at this

DIR=/tmp
MAXLEN=50 # max length for filename

find $DIR -type f -name "*.abc" | \
while read FILENAME
do
BASE=$(basename $FILENAME)
if (( ${#BASE} > $MAXLEN ))
then
print "$FILENAME is greater then $MAXLEN characters"
fi
done



You may be disappointed if you fail, but you are doomed if you don't try
Sridhar Bhaskarla
Honored Contributor

Re: check valid filename length of all .abc files in current⊂ dir

Is there an end to my corrections?... Correction to my correction itself..

BASE=$(basename "$FILENAME")

Put $FILENAME in quotes so in case if there are any spaces in the filename, they will not be ignored.

Sorry about my multiple messages.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
Bill Hassell
Honored Contributor

Re: check valid filename length of all .abc files in current⊂ dir

You are correct about the need for basename. A faster method is to bypass basename (because it is a separate process) by using the shell's built-in basename:

BASE=${FILENAME##*/}

The built-in ## construct will handle space and other special characters.


Bill Hassell, sysadmin
Muthukumar_5
Honored Contributor

Re: check valid filename length of all .abc files in current⊂ dir

we can execute basename command with find as like ,

## Environment variable
DIR=/tmp/

echo "Enter the length of file length"
read LEGNTH

find $DIR -name "*.c" -exec basename {} \; | while read line; do
> [[ $(echo $line | awk '{ print length($1) }') -gt $LEGNTH ]] && echo "$line file is having filename length more than $LEGNTH"
> done

- Muthu
Easy to suggest when don't know about the problem!
Hein van den Heuvel
Honored Contributor

Re: check valid filename length of all .abc files in current⊂ dir

Muthu,

That doesn't add much to the prior replies, and go totaly the opposite of Bill's reply.

Bill suggests that for larger tasks it helps to minimize image activations. I second that advice. Too many shell scripts just brute-force the problems damn the costs.

Muthu's script for example tosses in 4 gratuiteous, needless, images activations, with just one conditional.
You _will_ notice this if you need to find a few dozen files amongst a hundreds of thousands.

'basename': The shell knows how to get a basename (as per earlier reply)
'awk': The shell knows how to find a string length (as per earlier reply)
2*'echo': The shell knows how to 'print' if you let it.

Ok, I'll get of my high horse now, too easy to fall. It's time to go to bed over here.

Cheers,
Hein.

$ which echo
/usr/bin/echo
$ file /usr/bin/echo
/usr/bin/echo: ELF-32 executable object file - IA64
$ which print
no print in ...
$ print test
test
$ echo test
test
$