The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
What the command to find out the record length of a fixed length file? tranq01 UNIX for Dummies Questions & Answers 9 12-04-2008 03:04 PM
Change All File Names in a Directory andou UNIX for Dummies Questions & Answers 8 01-22-2008 08:06 PM
Replace characters in all file names in a particular directory madhunk Shell Programming and Scripting 4 02-16-2006 06:10 PM
directory names in a flat file surjyap Shell Programming and Scripting 2 10-06-2005 07:51 AM
File and Directory Names become hidden dbinsol1 UNIX for Advanced & Expert Users 10 05-29-2002 09:58 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-02-2008
Registered User
 

Join Date: Jun 2007
Posts: 48
find the length of file names in a directory?

Hi,
how can find length of file names in a directory.

Examp:

I have a directory with name "d1".
directory: d1

files:
aaa.log
bbb.log
abcd.log
abcdef.log

I wold like out put like:
file name legnth
aaa.log 3
bbb.log 3
abcd.log 4
abcdef.log 5

Thanks
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-02-2008
Yogesh Sawant's Avatar
Part Time Moderator and Full Time Dad
 

Join Date: Sep 2006
Location: Rossem, Tazenda
Posts: 1,049
using bash:
Code:
for file in *
do
    ff=`echo $file | cut -d '.' -f1`
    echo -n "$ff "
    echo ${#ff}
done
Reply With Quote
  #3 (permalink)  
Old 06-02-2008
robotronic's Avatar
Can I play with madness?
 

Join Date: Apr 2002
Location: Italy
Posts: 370
Code:
ls | awk '$(NF+1)=length'

Last edited by robotronic; 06-02-2008 at 10:40 AM..
Reply With Quote
  #4 (permalink)  
Old 06-02-2008
Registered User
 

Join Date: Dec 2006
Posts: 6
Script to get file name length discarding dot extension

If I understand your question, the following should work or you can modify it to do what you need.


for i in `ls -1`
do
val=`echo $i | cut -d '.' -f1 | wc | tr '\t' ' ' | tr -s ' ' | sed 's/^ //' | cut -d' ' -f 3`
val=`/usr/bin/expr $val - 1`
echo $i $val
done
Reply With Quote
  #5 (permalink)  
Old 06-02-2008
robotronic's Avatar
Can I play with madness?
 

Join Date: Apr 2002
Location: Italy
Posts: 370
If you want to exclude the file extension from the character count, try this instead:

Code:
ls | awk -F'.' '{ print($0, NF>1?length-length($NF)-1:length) }'
Reply With Quote
  #6 (permalink)  
Old 06-04-2008
Registered User
 

Join Date: Dec 2006
Posts: 6
Robo - your method

is more elegant. In my case, I just never took the time to learn awk. Thanks for the alternative.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Bookmarks

Tags
awk

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:




All times are GMT -4. The time now is 08:30 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66