Register
It is currently Wed Aug 20, 2014 10:21 pm

directoryname=number, return subdirectory with highest name


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sat Nov 22, 2008 8:38 am   

Joined: Sat Nov 22, 2008 7:11 am
Posts: 1
I have a fairly irritating problem, which is probably easily solved.
A program makes a number of directories. with subdirectories in which it stores logfiles.
the subdirectories have numbers as names like this:

basedir
|-directory1
| |--233
| |-234
| |-235
|
|-directory2
| |-177
| |-178
| |-179
|
|-directory3
| |-55
| |-56
| |-57
etc.

I need a program that returns the name of the directory along with the highest numbered subdirectories.
for the example above, i would like a listing like this:

directory1:235
directory2:179
directory3:57

Can this be done in a fairly easy way?

(am trying to learn bash, but its a slow process. I am really appriciating the help, thanks)


Top
 Profile  
 PostPosted: Sun Nov 23, 2008 5:09 pm   

Joined: Mon Nov 17, 2008 7:25 am
Posts: 221
Code:
#!/bin/bash
for i in $(ls --color=no); do
   if [ -d $i ]; then
      previous=0
      for a in $(ls --color=no $i); do
         if [ $a -gt $previous ]; then
            previous=$a
         fi
      done
      echo "$i:$previous"
   fi
done

This will output it the way you want, but it doesn't handle normal files very well. Guess that's something you can sort out.

Best regards
Fredrik Eriksson


Top
 Profile  
 PostPosted: Fri Nov 28, 2008 11:37 pm   

Joined: Fri Nov 28, 2008 10:32 pm
Posts: 3
Code:
for d in *; do
    test -d "$d" && {
        ls "$d" | sort -rn | tr '\n' ' ' | (read a b; echo "$d: $a")
    }
done


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP