find the length of file names in a directory?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find the length of file names in a directory?
# 1  
Old 06-02-2008
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
# 2  
Old 06-02-2008
using bash:
Code:
for file in *
do
    ff=`echo $file | cut -d '.' -f1`
    echo -n "$ff "
    echo ${#ff}
done

# 3  
Old 06-02-2008
Code:
ls | awk '$(NF+1)=length'


Last edited by robotronic; 06-02-2008 at 11:40 AM..
# 4  
Old 06-02-2008
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
# 5  
Old 06-02-2008
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) }'

# 6  
Old 06-04-2008
Robo - your method

is more elegant. In my case, I just never took the time to learn awk. Thanks for the alternative.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

making find/sed to include directory names with spaces

how can i make find/sed to include directory names with spaces the command is like this for i in `find wp-content/themes -type f -print0 | xargs -0 grep -l -iE 'e'`;do sed -i -e 's/word1/word2/gI' "$i";done but it skips one directory names with spaces sed: can't read ./Nova: No such... (5 Replies)
Discussion started by: vanessafan99
5 Replies

2. Shell Programming and Scripting

How to find all files which has names in uppercase in a directory

i want to display all the files which has their names in the Uppercase in a particular directory...guide.. (6 Replies)
Discussion started by: sheelsadan
6 Replies

3. Shell Programming and Scripting

How to find empty files in a directory and write their file names in a text?

I need to find empty files in a directory and write them into a text file. Directory will contain old files as well, i need to get the empty files for the last one hour only. (1 Reply)
Discussion started by: vel4ever
1 Replies

4. Shell Programming and Scripting

Grepping file names, comparing them to a directory of files, and moving them into a new directory

got it figured out :) (1 Reply)
Discussion started by: sHockz
1 Replies

5. UNIX for Dummies Questions & Answers

find the file names having specified pattern at specified position in the current directory

I would need a command for finding first 15000 of the file names whose 25th postion is 5 in the current directory alone. I do have this painful command find . -name '5*' | head -15000 | cut -c3- please refine this. Of course the above command also searches in the sub directories... (3 Replies)
Discussion started by: vk39221
3 Replies

6. Shell Programming and Scripting

Searching for file names in a directory while ignoring certain file names

Sun Solaris Unix Question Haven't been able to find any solution for this situation. Let's just say the file names listed below exist in a directory. I want the find command to find all files in this directory but at the same time I want to eliminate certain file names or files with certain... (2 Replies)
Discussion started by: 2reperry
2 Replies

7. Shell Programming and Scripting

Find Directory from array of file names with paths

I have a script that generates a variable with the location of a file and its complete path. What i want to do is to "cd" to the directory where that file is located using the path name of the file. GIS has absolutely failed me. For example when i run my script it generates a variable called... (1 Reply)
Discussion started by: Knome
1 Replies

8. AIX

find for specific content in file in the directory and list only file names

Hi, I am trying to find the content of file using grep and find command and list only the file names but i am getting entire file list of files in the directory find . -exec grep "test" {} \; -ls Can anyone of you correct this (2 Replies)
Discussion started by: madhu_Jagarapu
2 Replies

9. UNIX for Dummies Questions & Answers

Listing full file names with the exact length of 3 characters

This is what I have to do: Display the full file name (including the full path) and file size of all files whose name (excluding the path) is exactly 3 characters long. This is the code I have: find / -printf "Name: %f Path: %h Size: %s (bytes)\n" 2>/dev/null | grep -E "Name: .{3,} Path" |... (7 Replies)
Discussion started by: Joesgrrrl
7 Replies

10. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies
Login or Register to Ask a Question