LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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-05-2009, 10:57 AM   #1
jonhanna
Member
 
Registered: Dec 2008
Posts: 31

Rep: Reputation: 15
Directory question?


What function do I write that takes a directory name as an argument and writes to standard output the maximum of the lengths of all filenames in that directory and if the function's argument is not a directory name, and what error message do I write to standard output and exit with nonzero status?
(second question is tied in with the first)
My second question is how do I modify this function to descend all subcategories of the named directory recursively and to find the maximum length of any filename in that hierarchy?

If you guys could point me in the right direction or something of that nature it would be greatly appreciated, thanks to all.
 
Old 01-05-2009, 11:08 AM   #2
diego_cn
Member
 
Registered: Mar 2006
Distribution: fedora 7
Posts: 62

Rep: Reputation: 16
If you try to get your homework done, here's a few hints:
man wc
man test
bash command substitution with `cmd`
man sort
 
Old 01-05-2009, 11:12 AM   #3
jonhanna
Member
 
Registered: Dec 2008
Posts: 31

Original Poster
Rep: Reputation: 15
If I could get a little more detail I would appreciate it, I will use those hints though, I am just pressed for time and have spent a week already on this, also I am not just trying to get my homework done I am trying to learn it as well, hence why I am here asking you guys.
 
Old 01-05-2009, 11:51 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Here's one bit that might help:

Code:
ls -lR|grep "^-"|awk '{print $5}'|sort -n
Translation:
ls -lR: Long listing, recursive (goes all the way to the bottom)
grep "^-": returns only the line beginning with "-"---i.e only regular files
awk '{print $5}': Returns the 5th field--ie the file size
sort -n: numeric sort

You probably should also look at "du".

What is your textbook for scripting? Very good free stuff is at http://tldp.org

Start with the Bash Guide for Beginners by Machtelt Garrels
 
Old 01-05-2009, 01:34 PM   #5
jonhanna
Member
 
Registered: Dec 2008
Posts: 31

Original Poster
Rep: Reputation: 15
thank you much, If anyone could go more into detail on this command and tell me if this is the command for the first part or the second, i believe it is the second but, if someone could explain the difference that would be great, thanks.
 
Old 01-05-2009, 02:01 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by jonhanna View Post
thank you much, If anyone could go more into detail on this command and tell me if this is the command for the first part or the second, i believe it is the second but, if someone could explain the difference that would be great, thanks.
If you run the command I gave you, you will see that it addresses your second question.

More detail:
I explained what each part of the command does. Please state specifically which part you don't understand.
 
Old 01-05-2009, 02:32 PM   #7
jonhanna
Member
 
Registered: Dec 2008
Posts: 31

Original Poster
Rep: Reputation: 15
ls -lR|grep "^-"|awk '{print $5}'|sort -n

I understand everything in the command except for, what is the awk {print 5}? also what part of the command is the part that finds the maximum length of any file name in that hierarchy?
 
Old 01-05-2009, 06:42 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
awk is printing the 5th field (look at "man awk" for the details of the syntax**)

I just realized you said "length of filename" and not "length of file" (ie size of file)

so we need to make some changes.......

Modify the awk statement to grab the "filename" field.

Then add "wc -m" before the sort. "wc -m" counts the number of characters.


Test each one of these commands before stringing them all together---make sure you know how each one works.

Did you get the document I recommended earlier?

**Really good tutorials on awk (and more) here: http://www.grymoire.com/Unix
 
Old 01-06-2009, 07:34 AM   #9
jonhanna
Member
 
Registered: Dec 2008
Posts: 31

Original Poster
Rep: Reputation: 15
yes i did get that doc and i saved it to my computer, what am i going to modify the awk to? I will try these commands thank you very much
 
Old 01-06-2009, 08:16 AM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by jonhanna View Post
yes i did get that doc and i saved it to my computer, what am i going to modify the awk to? I will try these commands thank you very much
Simply change the number in the AWK command to give you the desired field. Note that the default field separator is a space, but AWK has an option to change that.

Do some simple experiments using just this:
ls -l|awk '{print $X}'
Enter different values for "X" and see what you get.

Quote:
yes i did get that doc and i saved it to my computer
Now all you need to do is read it.....
 
Old 01-08-2009, 08:19 AM   #11
jonhanna
Member
 
Registered: Dec 2008
Posts: 31

Original Poster
Rep: Reputation: 15
thank you i believe i got it now.
 
  


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
directory question the_pheonix Linux - Newbie 9 11-12-2006 08:01 PM
Home Directory Question carlosinfl Debian 1 10-04-2006 01:42 PM
Directory stack question szp Linux - General 2 08-11-2006 06:21 AM
ProFTPD Directory Question signature16 Linux - Networking 1 05-24-2006 08:51 AM
Directory question ProtoformX Linux - Security 2 06-07-2004 02:32 PM


All times are GMT -5. The time now is 03:53 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration