#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2011
    Posts
    22
    Rep Power
    0

    List files using Loop


    I was hoping someone could tell me how to list the files in a directory one by one using a loop. I know it's possible to just put ls -l directory1, but I want to go through the files in the directory one by one and list them that way.

    My script file is called forloop

    for file in $(ls -a $1)
    do
    ls -l $1/$file
    done

    When I run: forloop junk1 I get a listing of ALL my files when I just want a listing of files in junk1.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2006
    Posts
    2,477
    Rep Power
    1752
    the -l (ell) option for ls is the long listing, so when the object of that is a directory, it will list the directory ... In the loop you could either use -ld or filter out directories in the loop control if you want to.
    The moon on the one hand, the dawn on the other:
    The moon is my sister, the dawn is my brother.
    The moon on my left and the dawn on my right.
    My brother, good morning: my sister, good night.
    -- Hilaire Belloc

IMN logo majestic logo threadwatch logo seochat tools logo