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 # ...
- 10-08-2010 #1Just 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.
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 directoryCode:#script lc if [ "$1" != "" ] then echo "Error: no arguments allowed." exit fi linecount=0 while read input do linecount=$((linecount+1)) done
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)Code:echo $1 ls -R $1 | ./lc #if [ -f $file1 ] ##test for file / dir.
- 10-08-2010 #2Just 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.
- 10-08-2010 #3Just 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?
- 10-09-2010 #4Just Joined!
- Join Date
- Jul 2008
- Posts
- 81
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.
- 10-09-2010 #5Linux 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)) done0 + 1 = 1 != 2 <> 3 != 4 ...
Until the camel can pass though the eye of the needle.


Reply With Quote