LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-04-2007, 09:00 AM   #1
dresch
LQ Newbie
 
Registered: Jan 2007
Posts: 5

Rep: Reputation: 0
Unhappy Longest filename in a directory


Can anyone help me to write a shell script that when given a directory as an argument, finds the longest filename in that directory and any subdirectories?
 
Old 01-04-2007, 10:21 AM   #2
ronkymac
Member
 
Registered: Nov 2004
Location: Edinburgh, Scotland, UK.
Distribution: Debian
Posts: 72

Rep: Reputation: 15
Hi,

After having searched the net for about 2 minutes, I came up with this, which should be of help:

http://www.linuxforums.org/forum/lin...postcount47550

Regards,
Ronald.
 
Old 01-04-2007, 10:33 AM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Try this:

Code:
#!/bin/bash
maxlen=0

for file in `find $1 -type f` ; do

    len=`echo "$file" | gawk -F\/ '{printf "%s", $NF}' | wc -c`
    [ $len -gt $maxlen ] && maxlen=$len && maxfile=$file
    
done

echo $len $maxfile
 
Old 01-04-2007, 07:48 PM   #4
dresch
LQ Newbie
 
Registered: Jan 2007
Posts: 5

Original Poster
Rep: Reputation: 0
longest filename is picking up path in word count

Thanks for the help.

However my code below does not quite work. It is looking in current directory and sub-directories but when looking for the longest filename is is counting the path to that file aswell.

Is there something I am leaving out of the code?

#!/bin/bash
# Given a directory name as argument
# print the longest filename in that directory.

name=${0##*/}
FILES=${1-'pwd'}
length=0
filename=''

if [ ! -d $directory ]; then
echo "'$directory' is not a directory"
echo "Usage: $name [directory]"
exit 1
fi

for file in `find $FILES`; do
len=${#file}
if [ $len -gt $length ]; then
length=$len
filename=$file
fi
done

echo "The longest filename in directory is $filename
[/SIZE][/SIZE][/FONT]with $length characters."[/SIZE]
 
Old 01-05-2007, 01:46 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Quote:
Originally Posted by dresch

However my code below does not quite work. It is looking in current directory and sub-directories but when looking for the longest filename is is counting the path to that file aswell.

for file in `find $FILES`; do
len=${#file}
if [ $len -gt $length ]; then
length=$len
filename=$file
fi
done
This is beacuse the find command give the full path of the files it finds. This is the reason why I parsed the output with gawk in the script above.

Code:
len=`echo "$file" | gawk -F\/ '{printf "%s", $NF}' | wc -c`
This echoes the full-path, extract the last field with gawk considering "/" as the field separator (and without the newline control-character, that would give a wrong result) and then count the characters in it.

Hope this will help...
 
Old 01-05-2007, 02:56 PM   #6
muha
Member
 
Registered: Nov 2005
Distribution: xubuntu, grml
Posts: 451

Rep: Reputation: 37
Quote:
Originally Posted by colucix
Try this:

Code:
#!/bin/bash
maxlen=0

for file in `find $1 -type f` ; do

    len=`echo "$file" | gawk -F\/ '{printf "%s", $NF}' | wc -c`
    [ $len -gt $maxlen ] && maxlen=$len && maxfile=$file
    
done

echo $len $maxfile
Shouldn't the last line be:
echo $maxlen $maxfile
Otherwise it will just spew out the last length ...
 
Old 01-05-2007, 03:29 PM   #7
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
My lazy way of doing this:
Code:
ls | gawk '{ print length, $0 }' | sort -rn | head -1
edit -- sorry, I missed the "must recurse" requirement at first read.

edit2 -- Here's my next stab at it.

Code:
find . -exec basename {} \; | gawk '{ print length, $0 }' | sort -rn | head -1
(note sure if -type f is another requirement for you; if so you can add it.)

Last edited by anomie; 01-05-2007 at 03:58 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert static library (Filename.a) to dynamic shared object (filename.so) afx2029 Linux - Software 4 08-17-2007 06:07 AM
strange filename/file in my /usr/bin directory M$ISBS Linux - Security 1 06-04-2006 07:55 PM
How do I stop the filename and directory from printing? BajaNick Linux - Hardware 1 08-31-2004 04:31 PM
read filename from directory newbielinux Linux - Newbie 2 03-02-2004 03:48 PM
Changing to a directory with a space in the filename? guitargeek Linux - General 5 02-13-2003 05:38 PM


All times are GMT -5. The time now is 08:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration