Unix help

Discussion in 'Mac Basics and Help' started by scan, Jan 30, 2006.

  1. scan macrumors 6502

    Joined:
    Oct 24, 2005
    #1
    Hi,

    does anyone know of the command (using ls i'm guessing) that would display the files in a directory with at least 2 dots (.)
     
  2. superbovine macrumors 68030

    superbovine

    Joined:
    Nov 7, 2003
    #2
    I don't know what 2 dots you are referring to, but "ls -al" will probably display what you want including hidden files that start with a dot.
     
  3. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #3
    ls -la *.*.*

    Although I don't think it will list anything starting with a .
     
  4. Doctor Q Administrator

    Doctor Q

    Staff Member

    Joined:
    Sep 19, 2002
    Location:
    Los Angeles
    #4
    If you want to see all files, except those starting with a period:
    ls​

    If you want to see all files, including those starting with a period:
    ls -a​

    If you want to see all files that contain two more more periods in their name, except those starting with a period:
    ls | grep '\.[^.]*\.'​

    If you want to see all files that contain two more more periods in their name, including those starting with a period:
    ls -a | grep '\.[^.]*\.'​

    The expression \.[^.]*\. means this:
    \. = a period
    [^.] = anything other than a period
    * = zero or more of the preceding item, i.e., zero or more non-periods
    \. = a period​
    So when you put them together it means two periods separated by zero or more non-periods, and that string can occur anywhere within the filename.

    If you want exactly two periods or other variations, the pattern can be tinkered.
     
  5. balamw Moderator

    balamw

    Staff Member

    Joined:
    Aug 16, 2005
    Location:
    New England
    #5
    As usual in unix there are many ways to skin a cat.

    How about:

    Code:
     find -name \*\.\*\.\* -print
    You need to escape the * and . so the shell doesn't try to interpret them.

    B
     
  6. scan thread starter macrumors 6502

    Joined:
    Oct 24, 2005
    #6
    thx.
    i have a new problem now.

    using grep i need to find lines with only 1 character
     
  7. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #7
    grep ^.$ filename

    Is this school work?
     
  8. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    grep '^.$' <filename>

    Damn. You beat me to it. You should quote the search string though as $ is meaningful to the shell...
     
  9. scan thread starter macrumors 6502

    Joined:
    Oct 24, 2005
    #9
    kind of. its an exercise. studying for a test

    can you explain this. i don't really get it
     
  10. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #10
    ^ = start of line
    . = Any character
    $ = end of line

    So we are looking for lines with one character (any) between the start and end.

    The quotes are because $ is the start of an environment variable and we don't want the shell to treat it as an environment variable.
     
  11. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #11
    It's just text if followed by space. I love the way the shell parses things, it's clearly the work of sick minds :)
     
  12. scan thread starter macrumors 6502

    Joined:
    Oct 24, 2005
    #12
    lines with only digits

    tried: grep [:digit:] * but doesn't work
     
  13. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #13
    Those character class things are special, even though they have brackets of their own they still need brackets to enclose them.

    So:

    grep [[:digit:]] *

    Or, the more traditional way:

    grep [0-9] *
     
  14. scan thread starter macrumors 6502

    Joined:
    Oct 24, 2005
    #14
    tried that already. the problem is it included lines with digits as well as letters. I need lines with only digits
     
  15. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #15
    Have you considered reading the documentation. Or learning from the previous examples.

    grep '^[0-9]*$' <filename>

    will print lines that consist of only numbers. You could add whitespace as well if you want.
     
  16. scan thread starter macrumors 6502

    Joined:
    Oct 24, 2005
    #16
    thanks

    i use the man pages but nothing really helped me with this question. We also not given any resources so its kind of "figure it out on your own" type of deal.
     
  17. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #17
    A google search for "grep tutorial" gave lots of hits. This was the first one.
     
  18. iMeowbot macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #18
    To do that in one pass, you would probably want to use egrep.

    egrep '^[0-9]+$' filename

    The ^ and $ mean beginning and end of line as before, and the + means to find one or more occurrence of the preceding expression.

    =-=-=-=

    If you really need to use non-extended grep, you could also do it in two passes, once to find digits and again to filter out non-digits:

    grep [0-9] filename | grep -v '[^0-9]'

    The first part, grep [0-9] , finds lines containing digits.

    The second part, grep '[^0-9]' , finds lines containing things that aren't digits. Because the -v flag is on there, we instead get the opposite (lines not containing non-digit characters).

    That will return empty lines too :( ^[0-9][0-9]*$ would do the right thing though.
     
  19. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #19
    Yes I suppose it will. Still I leave perfecting the answer to the reader :D
     
  20. savar macrumors 68000

    savar

    Joined:
    Jun 6, 2003
    Location:
    District of Columbia
    #20
    This site is ridiculously helpful:

    http://www.regular-expressions.info/tutorial.html
     
  21. superbovine macrumors 68030

    superbovine

    Joined:
    Nov 7, 2003
    #21
  22. fisty macrumors member

    Joined:
    Jan 30, 2006
    #22
    for any command in unix there is something VERY usefull command "man"
    just type man "command youd like to know more about"
    e.g. man ls

    it opens a manual to that particular command usually....hope it helps
     
  23. scan thread starter macrumors 6502

    Joined:
    Oct 24, 2005
    #23
    How can I use "find" command to list the subdirectories of a directory??
     
  24. fisty macrumors member

    Joined:
    Jan 30, 2006
    #24

    dude....
    read what i posted....grrrrrrrrrrrrrrrr


    i hate when people just dont know anything and want the easy way....i told ya how to find out whatever u like on any unix box MAN command....if that isnt clear enough or well too complicated google it, there are plenty of websites for beginners on the matter....

    its not entirly directed at you....but £%£$^"£ try and find out for urself these little things....u know gives ya some reward too by finding out things urself and get a few things self thought rather than just ask on a forum where you have to wait....

    if you would have googled this information any questiens on that regard you would have known by now and even more than me by the time im actually typing this msg...

    dont want to sound mean....but hey cmon....you have the bigest bloody library infront of your eyes when u stare at your monitor USE IT
     
  25. scan thread starter macrumors 6502

    Joined:
    Oct 24, 2005
    #25
    "dude" if you read any of my earlier posts I DO USE THE MAN PAGES.

    if i'm asking for help, beleive me, i've already looked there. I also googled for help too. I'm not looking for any kind of easy way out since i'm trying to learn this stuff. you don't want to help, dont' bother posting.

    I can list the subdirectories and files but I think what I want is just the subdirectories, which I don't know how to do.

     

Share This Page