Find the answer to your Linux question:
Results 1 to 7 of 7
Hi all , i have the following situation, a directory contains something like : linux v1r0p2 linux v3r7p2 linux v4r6p9 and so on, i want to write a script can ...
  1. #1
    Linux Engineer aliov's Avatar
    Join Date
    Dec 2006
    Location
    Geneva,Beirut
    Posts
    1,078

    finding out last version with bash

    Hi all , i have the following situation, a directory contains something like :

    linux v1r0p2
    linux v3r7p2
    linux v4r6p9

    and so on, i want to write a script can grab me the last version, i've done something, but it a bit complicated and long, i'm wondering if there is a combination of sed and awd can do it easily.
    Linux is not only an operating system, it's a philosophy.
    Archost.

  2. #2
    Linux Newbie
    Join Date
    Nov 2007
    Location
    Planet Earth
    Posts
    152
    Code:
    ls -1 | sort -r | head -1


    (only if the version strings have the same length... I think)
    EOF

  3. #3
    Linux Engineer aliov's Avatar
    Join Date
    Dec 2006
    Location
    Geneva,Beirut
    Posts
    1,078
    Code:
    ls -l  | grep -w 'linux' | sort -r | head -1
    I use it like this and it seems it's working , thanks a lot .

    Cheers.
    Linux is not only an operating system, it's a philosophy.
    Archost.

  4. #4
    Linux Enthusiast
    Join Date
    Aug 2006
    Location
    Portsmouth, UK
    Posts
    539
    If the latest version is always the last one created, add "t" to the ls command to sort by time as well, the "t" and "r" flags to ls imply sorting so you don't need to pipe it through the sort command:

    Code:
    ls -ltr linux* | head -1
    Edit:
    You don't need grep either...
    RHCE #100-015-395
    Please don't PM me with questions as no reply may offend, that's what the forums are for.

  5. #5
    Linux Engineer aliov's Avatar
    Join Date
    Dec 2006
    Location
    Geneva,Beirut
    Posts
    1,078
    Quote Originally Posted by matonb View Post
    If the latest version is always the last one created
    This is not true, at least in my case, some times there is some modification done in older verions, that's why i didn't use -ltr flags.
    Linux is not only an operating system, it's a philosophy.
    Archost.

  6. #6
    Linux Enthusiast
    Join Date
    Aug 2006
    Location
    Portsmouth, UK
    Posts
    539
    In that case drop the "t"...
    RHCE #100-015-395
    Please don't PM me with questions as no reply may offend, that's what the forums are for.

  7. #7
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by matonb View Post
    If the latest version is always the last one created, add "t" to the ls command to sort by time as well, the "t" and "r" flags to ls imply sorting so you don't need to pipe it through the sort command:

    Code:
    ls -ltr linux* | head -1
    To be exactly equivalent, you'd need
    Code:
    ls -lr *linux* | head -1
    since grep will match 'linux' anywhere in the name, not just at the beginning. And, of course, you don't need the -l if you only want the filename - remember that ls writes filenames one per line unless it's writing to a terminal (so you don't need the -1 (that's a one, not an L) that hugortega used in his example).

    Also be aware that it'll break after v9 since v10 will sort after v1 but before v2.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...