Interpretation of UNIX command


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Interpretation of UNIX command
# 1  
Old 09-23-2016
Interpretation of UNIX command

what does the below do.
Code:
echo * | xargs ls | wc –l



echo * - Output a string comprising the name of each file in the working directory, with each name separated by a space.

xargs ls - construct argument list command

wc -l - it will pipe the output to the wc command, which will display a count of the number of words in the output

Last edited by vbe; 09-23-2016 at 04:35 AM.. Reason: code tags please even for so lttle...
# 2  
Old 09-23-2016
The first pipe is an expensive (and error prone) way to mimic the behaviour of ls *; not sure what it should be good for.
wc -l yields the count of lines, not words.
Please note that options to any command are introduced by a - (minus sign), NOT a (Unicode U+2014, "EM DASH") character which leads to a syntax error.

Last edited by RudiC; 09-23-2016 at 08:39 AM..
# 3  
Old 09-23-2016
Why not just:-
Code:
ls -1 | wc -l

This negates the need for xargs entirely.

Like RudiC says, using echo * is prone to error.



Robin
# 4  
Old 09-23-2016
Another way to count the number of (non-hidden) entries in a directory in pure shell, which is also more accurate (it will count filenames that contain newlines correctly):
Code:
set -- *; echo $#

or
Code:
nof () { echo $# ;}; nof *

Unless the directory is empty. Then these options report 1 instead of zero..



To mitigate these corner cases, in bash 4 one can use :
Code:
shopt -s nullglob

so that * expands to the null string if the directory is empty (except for hidden files).

or one could try something like:

Code:
nof () { 
  if [ -e "$1" ]; then
    echo $#
  else
    echo 0
  fi
}
nof *



--
Quote:
Originally Posted by rbatte1
Why not just:-
Code:
ls -1 | wc -l

This negates the need for xargs entirely.
[..]
Note: this could be shortened still to
Code:
ls | wc -l

Since -1 is the default option of ls when the output is not a terminal

Last edited by Scrutinizer; 09-24-2016 at 02:34 AM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 5  
Old 09-26-2016
Quote:
Originally Posted by rbatte1
Code:
ls -1 | wc -l

Just a small observation on the side: "-1" is unnecessary in this case, because ls will format its output in only one (instead of several) columns already if it notices that the output is not going to a terminal. See the following transcript:

Code:
# uname -srv
AIX 1 7

# for i in a b c d e ; do touch $i ; done

# ls -l
total 0
-rw-r--r--    1 bakunin     staff             0 Sep 26 17:27 a
-rw-r--r--    1 bakunin     staff             0 Sep 26 17:27 b
-rw-r--r--    1 bakunin     staff             0 Sep 26 17:27 c
-rw-r--r--    1 bakunin     staff             0 Sep 26 17:27 d
-rw-r--r--    1 bakunin     staff             0 Sep 26 17:27 e

# ls
a  b  c  d  e

# ls | cat -
a
b
c
d
e

I hope that helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Truss output interpretation

hi, anyone can help on this piece of truss output? 8094: 0.7028 write(4, 0x0043BE90, 236) = 236 8094: T S H \0\0\0EC020101\0\0\0\0\0\0\0\0\0 "02\0\0 303\0\0 I D 8094: \f %\0\0\0\0 2\0F67F\0\0\0\0 @06FFC99A ; 8094: L D6\0 303 8094: ... (6 Replies)
Discussion started by: ghostdog74
6 Replies

2. AIX

lspath output interpretation

On my VIo I see the following for my disks: $ lspath | grep hdisk6 Enabled hdisk6 fscsi0 200600a0b82193f7,4000000000000 Enabled hdisk6 fscsi0 200700a0b82193f7,4000000000000 Enabled hdisk6 fscsi2 200600a0b82193f8,4000000000000 Failed hdisk6 fscsi2 200700a0b82193f8,4000000000000 $ lspath |... (8 Replies)
Discussion started by: petervg
8 Replies

3. Shell Programming and Scripting

Interpretation needed for gawk command

Hi All I am running a insatll script in linux which installs the project. Could you please help in interpreting this command gawk '{ if (substr($1,0,1) == "\047") gsub("^\047+|\047+$", "", $1); print }' where $1 = BBME Thanks (1 Reply)
Discussion started by: vee_789
1 Replies

4. AIX

methods of command interpretation in AIX

what are the various methods through which a command is interpreted in AIX? (1 Reply)
Discussion started by: AIXlearner
1 Replies

5. AIX

interpretation of sar

hello with a sar i have this result: System configuration: lcpu=48 ent=4.00 14:06:37 %usr %sys %wio %idle physc %entc 14:06:39 26 9 3 62 1.63 40.7 14:06:41 26 9 3 63 1.58 39.4 14:06:43 ... (0 Replies)
Discussion started by: pascalbout
0 Replies

6. UNIX for Advanced & Expert Users

SAR -b interpretation

I have used SAR -b to get some Unix cache / buffer metrics and the results are confusing me a bit. The pread/s & pwrit/s are showing 0. However the lread/s and lwrit/s are showing figures. I note also that the bread/s and bwrit/s are showing figures. I believe that pread/s and pwrit/s is not... (3 Replies)
Discussion started by: jimthompson
3 Replies

7. UNIX for Dummies Questions & Answers

Interpretation of the uptime command

Hi there, do someone have detailed information how to interpret the uptime command or rather which values can be called normal? (i know what the information means, but i have no idea if these values are ok or to high: 3:02pm an 13:53, 2 Benutzer, Durchschnittslast: 10,06, 12,05, 13,00) ... (5 Replies)
Discussion started by: odin1999
5 Replies

8. UNIX for Dummies Questions & Answers

Prevent bash from interpretation :

I am using bash shell; my requirement is to run a long command. Now I have split this long command into a number of shell variables. Some of these shell variables contain special character ':' At the end, when the intended long command is executed as a series of small shell variables the ':'... (7 Replies)
Discussion started by: uday
7 Replies

9. UNIX for Dummies Questions & Answers

shell interpretation

I executed the following command in the korn shell: $ variable1="qwerty" ls | sort and the shell executed the 'ls | sort' command. I would have expected an error message from the shell, but instead of that the shell ran the 'ls | sort' command and didn't realize the variable assignement. ... (1 Reply)
Discussion started by: PhilippeCrokaer
1 Replies
Login or Register to Ask a Question