`wc`, `xargs`
why are the following two different -
Code:
find . -type d | xargs -t -n1 ls Code:
find . -type d | xargs -t -n1 wc -c The `wc` however does not run as I expected. So, I tried something simpler like Code:
wc -m 'foo bar' The `wc` manpage notes that stdin is read in the absence of a filename, so this must be something real minor that I'm getting wrong? Thanks. |
Code:
find . -type d | xargs -t -n1 wc -c |
Quote:
Which brings me to a related question. How can we use wc to run on a string on the cli? For eg: wc -c "foo bar"? I know we could do a wc -c << EOF, and then terminate it for the result, but is there another way? This thing's so basic, I'm surprised at my inability. heh. |
Ok, sorry. Now it is clear what you are trying to accomplish. The wc command is suited to work on files, not strings. You may workaround this behaviour in different ways, but it would be better using shell, as in
Code:
find . -type d | xargs -n1 expr length |
Quote:
Code:
$ find . -type d | wc -l |
Quote:
Quote:
The manpage for `echo` has an example for finding the depth of a file using `echo $PWD | tr / ' ' | wc -w`. This is precisely what I want to do, but on a dir tree. So, I tried `find . -type d | tr / ' '`. This would give us, in a line of words, the list of all dirs. The commands I'm trying on are POSIX versions, and don't have the same kind of options as many of us might have. For eg: there's a %d directive in `find` in my version, that lists the depth. There's the print0 switch to null-terminate. But I don't have these on the version I'm trying to work on. :) |
Quote:
|
so, this is what I'm trying to do -
Code:
echo /foo/bar/moo/cows/ | tr / ' ' | wc -w Code:
find /some/dir -type f | tr / ' ' | xargs -i wc -w {} Anyone? |
I think there are not many chances to achieve the result by means of the wc command. When you pipe the output from echo, or from any other command, wc treats the text as a whole, but if you pass a list of items wc interpret them as files to be processed. The wanted result can be obtained by cycling over the output of find, e.g.
Code:
for string in `find /some/dir -type f` Code:
find /some/dir -type f | awk -F/ '{print NF - 1}' |
If I am reading your correctly, you want a count of all the directory components in a given hierarchy?
Code:
$ find . -print | awk -F '/' 'BEGIN {sum=0;} {sum += NF} END {print sum}' |
Quote:
I decided to go with - Code:
for i in `find /some/path/ -type f`; do echo $i | tr / ' ' | wc -w; done Thanks anyway. |
Quote:
Thanks again. |
All times are GMT -5. The time now is 11:51 AM. |