Find the answer to your Linux question:
Results 1 to 5 of 5
Hi. Trying to use ls to list all files in a directory and sub-directories, but not show the actual directories. I have written a small script to count the # ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2010
    Posts
    2

    LS -R --ignore directories?


    Hi. Trying to use ls to list all files in a directory and sub-directories, but not show the actual directories. I have written a small script to count the # of lines within these files, but because of the way ls -R works, the script counts sub directories as a line.

    the below is to be used either alone (keyboard entered lines counted) or have some input redirected to it.
    Code:
    #script lc
    if [ "$1" != "" ]
            then
                    echo "Error: no arguments allowed."
                    exit
    fi
    
    linecount=0
    while read input
    do
            linecount=$((linecount+1))
    done
    this is "rtlc", which is to get some files in the dir, and subdirs, and then call lc to count the lines. args requirement is a directory

    Code:
    echo $1
    
    ls -R $1 | ./lc
    
    
    #if [ -f $file1 ] ##test for file / dir.
    I can't make this thing not count the sub directories as lines. Am I missing an ls option or char that can eliminate these? Scope does not allow for any external commands (like grep out the directories or use find)

  2. #2
    Just Joined!
    Join Date
    Oct 2010
    Posts
    14
    can you pipe the ls into grep and use the following switch

    -v
    --invert-match
    Invert the sense of matching, to select non-matching lines.

  3. #3
    Just Joined!
    Join Date
    Oct 2010
    Posts
    2
    I wish. Project specs say no external commands are to be used aside from LS. I can do ls -Rp to show / in the directory names, maybe output to a file, loop iterate the file and skip lines containing "/", but this of course uses external commands as well.

    Thoughts?

  4. #4
    Just Joined!
    Join Date
    Jul 2008
    Posts
    93
    Do you get to edit the "lc" script? That would be a place to reject directory names. Also to reject blank lines, which accompany each directory name.

    Otherwise I think you have a "you can't get there from here" project spec.

  5. #5
    Linux User
    Join Date
    Nov 2009
    Location
    France
    Posts
    292
    You could build from the following to reject all directory names (ending by /) and descriptions (ending by without using any external commands, it's essentially parsing the output of ls.

    Code:
    l=$(ls -R -1 ~/tmp)
    for f in $l;do
     ln=${#f}
     ln=$((ln-1))
    #GET THE LAST CHAR
     lst=${f:$ln}
     [ "$lst" != ":" ] && [ "$lst" != "/" ] && echo $f && linecount=$((linecount+1))
    done
    0 + 1 = 1 != 2 <> 3 != 4 ...
    Until the camel can pass though the eye of the needle.

Posting Permissions

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