Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Interpretation of UNIX command Post 302982102 by Scrutinizer on Friday 23rd of September 2016 06:42:32 PM
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:
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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. 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

7. 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

8. 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

9. 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
All times are GMT -4. The time now is 03:41 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy